Sunteți pe pagina 1din 6

DATA cars1;

INPUT make $ model $ mpg weight price;


CARDS;
AMC Concord 22 2930 4099
AMC Pacer 17 3350 4749
AMC Spirit 22 2640 3799
Buick Century 20 3250 4816
Buick Electra 15 4080 7827
;
RUN;
After reading in the data with a data step, it is usually a good idea to print the first few cases of
your dataset to check that things were read correctly.
title "cars1 data";
PROC PRINT DATA=cars1(obs=5);
RUN
Here is the output produced by the proc print statement above.

cars1
OBS
1
2
3
4
5

data
MAKE
AMC
AMC
AMC
Buick
Buick

MODEL
Concord
Pacer
Spirit
Century
Electra

MPG
22
17
22
20
15

WEIGHT
2930
3350
2640
3250
4080

PRICE
4099
4749
3799
4816
7827

DATA cars5;
INFILE "c:\carsdata\cars5.dat" delimiter=',';
INPUT make $ model $ mpg weight price;
RUN;
TITLE "cars5 data";
PROC PRINT DATA=cars5(OBS=5);
RUN;

cars5 data
OBS
1
2
3
4
5

MAKE
AMC
AMC
AMC
Buick
Buick

MODEL
Concord
Pacer
Spirit
Century
Electra

MPG
22
17
22
20
15

WEIGHT
2930
3350
2640
3250
4080

PRICE
4099
4749
3799
4816
7827

data prep;
length name $16;
set sashelp.class;
gender = sex; run;

DATA= specifies the dataset to be processed, PROMPT invokes the prompting mode, sort of
like a wizard NOWINDOWS suppresses the REPORT window and directs the report to the output
window REPORT = specifies a stored report to be used in generating a new report OUTREPT=
names a location to store the report OUT= creates a SAS data set HEADLINE creates a
horizontal line between the column headers and the body of the report HEADSKIP creates a
blank line between the column headers and the body of the report
PROC REPORT NOWD DATA=SURVEY HEADLINE HEADSKIP CENTER MISSING;
COLUMNS FNAME LINIT NAME SEX ACADYEAR GPA AGE

('-ANNUAL AMOUNT SPENT ON-' BOOKS FOOD ENT TOTAL);


DEFINE FNAME / DISPLAY NOPRINT;
DEFINE LINIT / DISPLAY NOPRINT;
DEFINE SEX / DISPLAY FORMAT=$SEXFMT. WIDTH=6;
DEFINE ACADYEAR / DISPLAY FORMAT=$YEARFMT. WIDTH=9 'ACADEMIC/YEAR';
DEFINE GPA / ANALYSIS FORMAT=3.1 WIDTH=3;
DEFINE AGE / ANALYSIS FORMAT=3. WIDTH=3;
DEFINE BOOKS / ANALYSIS FORMAT=DOLLAR6. WIDTH=7 'BOOKS';
DEFINE FOOD / ANALYSIS FORMAT=DOLLAR6. WIDTH=6 'FOOD';
DEFINE ENT / ANALYSIS FORMAT=DOLLAR6. WIDTH=11 'ENTERTAIN';
DEFINE NAME / COMPUTED WIDTH=10;
DEFINE TOTAL / COMPUTED FORMAT=DOLLAR6. WIDTH=7 'TOTAL';
COMPUTE NAME / CHAR LENGTH=10;
NAME=TRIM(FNAME) || ' ' || LINIT || '.';
ENDCOMP;
COMPUTE TOTAL;
TOTAL = SUM(BOOKS.SUM, FOOD.SUM, ENT.SUM);
ENDCOMP;
RUN;
PROC REPORT NOWD DATA=SURVEY HEADLINE HEADSKIP CENTER MISSING;
COLUMNS FNAME LINIT NAME SEX ACADYEAR GPA AGE
('-ANNUAL AMOUNT SPENT ON-' BOOKS FOOD ENT TOTAL);
DEFINE FNAME / DISPLAY NOPRINT;
DEFINE LINIT / DISPLAY NOPRINT;
DEFINE SEX / DISPLAY FORMAT=$SEXFMT. WIDTH=6;
DEFINE ACADYEAR / DISPLAY FORMAT=$YEARFMT. WIDTH=9 'ACADEMIC/YEAR';
DEFINE GPA / ANALYSIS FORMAT=3.1 WIDTH=3;
DEFINE AGE / ANALYSIS FORMAT=3. WIDTH=3;
DEFINE BOOKS / ANALYSIS FORMAT=DOLLAR6. WIDTH=7 'BOOKS';
DEFINE FOOD / ANALYSIS FORMAT=DOLLAR6. WIDTH=6 'FOOD';
DEFINE ENT / ANALYSIS FORMAT=DOLLAR6. WIDTH=11 'ENTERTAIN';
DEFINE NAME / COMPUTED WIDTH=10;
DEFINE TOTAL / COMPUTED FORMAT=DOLLAR6. WIDTH=7 'TOTAL';
COMPUTE NAME / CHAR LENGTH=10;
NAME=TRIM(FNAME) || ' ' || LINIT || '.';
ENDCOMP;
COMPUTE TOTAL;
TOTAL = SUM(BOOKS.SUM, FOOD.SUM, ENT.SUM);
ENDCOMP;
RUN;
PROC SORT DATA = SURVEY;
BY ACADYEAR;
FORMAT ACADYEAR $YEARFMT.;
RUN;
The REPORT procedure also has many options that can be used. Some of the most often used
options are:
DATA= specifies the dataset to be processed,
PROMPT invokes the prompting mode, sort of like a wizard
NOWINDOWS suppresses the REPORT window and directs the report to the output window
REPORT = specifies a stored report to be used in generating a new report
OUTREPT= names a location to store the report
OUT= creates a SAS data set
HEADLINE creates a horizontal line between the column headers and the body of the report
HEADSKIP creates a blank line between the column headers and the body of the report

GROUP - puts observations into categories


DISPLAY - displays values for each observation
ANALYSIS - contributes values to a calculation or statistic
ORDER - defines the order of the report rows
ACROSS - creates columns for each of its values
COMPUTED - its values are created in a COMPUTE block.
Data set one

Data set two

ID A B

ID C

10 1 2
20 3 4
30 5 6

10 0
20 5
30 7

This data step does a merge of data set one and two by ID:
data three;
merge one two;
by id;
run;
PROC PRINT DATA=auto;
WHERE rep78 >= 3;
VAR make rep78;
RUN;
PROC PRINT DATA=auto;
WHERE rep78 >= 3;
VAR make rep78;
RUN;
PROC FREQ DATA=auto;
TABLES rep78*foreign ;
RUN;
PROC FREQ DATA=auto;
WHERE rep78 >= 3;
TABLES rep78*foreign ;
RUN;
PROC PRINT DATA=auto;
WHERE rep78 <= 2;
VAR make price rep78 ;
RUN;
PROC PRINT DATA=auto;
WHERE rep78 <= 2 and rep78 ^= . ;
VAR make price rep78 ;
RUN;
PROC PRINT DATA=auto;
WHERE . < rep78 <= 2;
VAR make price rep78 ;
RUN;

PROC MEANS DATA=auto;


WHERE rep78 = 1 OR rep78 = 2 ;
VAR price ;
RUN;
PROC MEANS DATA=auto;
WHERE rep78 = 3 or rep78 = 4 or rep78 = 5 ;
VAR price ;
RUN;
Or:
PROC MEANS DATA=auto;
WHERE 3 <= rep78 <= 5 ;
VAR price ;
RUN;
Or: The where statement also works with the in operator.
PROC MEANS DATA=auto;
WHERE rep78 in (3,4,5) ;
VAR price ;
RUN;
DATA auto2;
SET auto;
repair = .;
IF (rep78=1) or (rep78=2) THEN repair = 1;
IF (rep78=3) THEN repair = 2;
IF (rep78=4) or (rep78=5) THEN repair = 3;
himpg = .;
IF (mpg <= 20) THEN himpg = 0;
IF (mpg > 20) THEN himpg = 1;
RUN;
DATA auto;
INPUT make $ price mpg rep78 foreign;
cost = ROUND( price / 1000 );
mpgptd = mpg / price;
DATALINES;
AMC 4099 22 3 0
;
RUN;
PROC PRINT DATA=auto;
RUN;

S-ar putea să vă placă și