lecture 10-intro to sas
DESCRIPTION
Introduction to SASTRANSCRIPT
11
Introduction to SAS Introduction to SAS
Lecture 2Lecture 2
September 28, 2004September 28, 2004
22
Why use statistical Why use statistical packagespackages
Built-in functionsBuilt-in functions Data manipulationData manipulation Updated often to include new Updated often to include new
applicationsapplications Different packages complete certain Different packages complete certain
tasks more easily than otherstasks more easily than others Packages we will introducePackages we will introduce
– SASSAS– R (S-plus)R (S-plus)
33
SASSAS
Easy to input and output data setsEasy to input and output data sets Preferred for data manipulationPreferred for data manipulation ““proc” used to complete analyses with proc” used to complete analyses with
built-in functionsbuilt-in functions Macros used to build your own functionsMacros used to build your own functions SAS StructureSAS Structure Efficient SAS Code for Large FilesEfficient SAS Code for Large Files SAS Macro FacilitySAS Macro Facility
44
Common errorsCommon errors
Missing semicolonMissing semicolon MisspellingMisspelling Unmatched quotes/commentsUnmatched quotes/comments Mixed proc and data statementMixed proc and data statement Using wrong optionsUsing wrong options
55
SAS StructureSAS Structure
Data Step: input, create, manipulate Data Step: input, create, manipulate or output dataor output data– Always start with a data lineAlways start with a data line– Ex. Ex. data one;data one;
Procedure Step: complete an Procedure Step: complete an operation on dataoperation on data– Always start with a proc lineAlways start with a proc line– Ex. Ex. proc contents;proc contents;
66
Statements for Reading Statements for Reading DataData
data data statement names the data set statement names the data set you are makingyou are making
Can use any of the following Can use any of the following commands to input datacommands to input data– infileinfile Identifies an external raw data file Identifies an external raw data file
to read with an INPUT statement to read with an INPUT statement – inputinput Lists variable names in the input Lists variable names in the input
filefile– cardscards Indicates internal data Indicates internal data – setset Reads a SAS data setReads a SAS data set
77
ExampleExample
data temp; data temp;
infile ‘g:\shared\BIO271summer\baby.csv' infile ‘g:\shared\BIO271summer\baby.csv' delimiter=',' dsd;delimiter=',' dsd;
input input id headcir length bwt gestwks mage id headcir length bwt gestwks mage mnocig mheight mppwt fage fedyrs fnocig mnocig mheight mppwt fage fedyrs fnocig fheigfheig; ;
run; run;
proc print data = temp (obs=10); proc print data = temp (obs=10);
run; run;
88
Delimiter OptionDelimiter Option
blank space (default)blank space (default) DELIMITER= option specifies that the DELIMITER= option specifies that the
INPUT statement use a character INPUT statement use a character other than a blank as a delimiter for other than a blank as a delimiter for data values that are read with list data values that are read with list input input
99
Delimiter ExampleDelimiter Example
Sometimes you want to input the data Sometimes you want to input the data yourselfyourself
Try the following data step:Try the following data step:data nums; data nums; infile datalines dsd delimiter=‘&'; infile datalines dsd delimiter=‘&'; input X Y Z; input X Y Z; datalines; datalines; 1&2&3 1&2&3 4&5&6 4&5&6 7&8&9 ;7&8&9 ;
Notice that there are no semicolons until the Notice that there are no semicolons until the end of the datalinesend of the datalines
1010
DSD optionDSD option
Change how SAS treats delimiters when list input Change how SAS treats delimiters when list input is used and sets the default delimiter to a is used and sets the default delimiter to a comma. When you specify DSD, SAS treats two comma. When you specify DSD, SAS treats two consecutive delimiters as a missing value and consecutive delimiters as a missing value and removes quotation marks from character values. removes quotation marks from character values.
Use the DSD option and list input to read a Use the DSD option and list input to read a character value that contains a delimiter within a character value that contains a delimiter within a quoted string. The INPUT statement treats the quoted string. The INPUT statement treats the delimiter as a valid character and removes the delimiter as a valid character and removes the quotation marks from the character string before quotation marks from the character string before the value is stored. Use the tilde (~) format the value is stored. Use the tilde (~) format modifier to retain the quotation marks.modifier to retain the quotation marks.
1111
Example: Reading Delimited Example: Reading Delimited DataData
SAS data step:SAS data step:data scores;data scores; infile datalines delimiter=',';infile datalines delimiter=','; input test1 test2 test3;input test1 test2 test3; datalines;datalines;91,87,9591,87,9597,,9297,,92,1,1,1,1;;
Output:Output: Obs test1 test2 test3Obs test1 test2 test3 1 91 87 951 91 87 95 2 97 92 12 97 92 1
1212
Example: CorrectionExample: Correction
SAS data stepSAS data stepdata scores;data scores;
infile datalines delimiter=',‘ infile datalines delimiter=',‘ dsd;dsd; input test1 test2 test3;input test1 test2 test3; datalines;datalines;91,87,9591,87,9597,,9297,,92,1,1,1,1;;
Output:Output: Obs test1 test2 test3Obs test1 test2 test3 1 91 87 951 91 87 95 2 97 . 922 97 . 92 3 . 1 13 . 1 1
1313
Modified List InputModified List Input
Read data that are separated by commas and Read data that are separated by commas and that may contain commas as part of a that may contain commas as part of a character value: character value:
data scores; data scores; infile datalines dsd; infile datalines dsd; input Name : $9. Score Team : $25. Div $; input Name : $9. Score Team : $25. Div $; datalines; datalines; Joseph,76,"Red Racers, Washington",AAAJoseph,76,"Red Racers, Washington",AAAMitchel,82,"Blue Bunnies, Richmond",AAA Mitchel,82,"Blue Bunnies, Richmond",AAA Sue Ellen,74,"Green Gazelles, Atlanta",AA Sue Ellen,74,"Green Gazelles, Atlanta",AA ;;
1414
Modified List InputModified List Input
Output:Output:
Obs Name Score Team DivObs Name Score Team Div
1 Joseph 76 Red Racers, Washington AAA1 Joseph 76 Red Racers, Washington AAA
2 Mitchel 82 Blue Bunnies, Richmond AAA2 Mitchel 82 Blue Bunnies, Richmond AAA
3 Sue Ellen 74 Green Gazelles, Atlanta AA3 Sue Ellen 74 Green Gazelles, Atlanta AA
1515
Dynamic Data Exchange Dynamic Data Exchange (DDE)(DDE)
Dynamic Data Exchange (DDE) is a method of Dynamic Data Exchange (DDE) is a method of dynamically exchanging information between Windows dynamically exchanging information between Windows applications. DDE uses a client/server relationship to applications. DDE uses a client/server relationship to enable a client application to request information from enable a client application to request information from a server application. In Version 8, the SAS System is a server application. In Version 8, the SAS System is always the client. In this role, the SAS System requests always the client. In this role, the SAS System requests data from server applications, sends data to server data from server applications, sends data to server applications, or sends commands to server applications, or sends commands to server applications. applications.
You can use DDE with the DATA step, the SAS macro You can use DDE with the DATA step, the SAS macro facility, SAS/AF applications, or any other portion of facility, SAS/AF applications, or any other portion of the SAS System that requests and generates data. the SAS System that requests and generates data. DDE has many potential uses, one of which is to DDE has many potential uses, one of which is to acquire data from a Windows spreadsheet or database acquire data from a Windows spreadsheet or database application. application.
1616
Dynamic Data Exchange Dynamic Data Exchange (DDE)(DDE)
NOTAB is used only in the context of NOTAB is used only in the context of Dynamic Data Exchange (DDE). This Dynamic Data Exchange (DDE). This option enables you to use nontab option enables you to use nontab character delimiters between character delimiters between variables. variables.
1717
DDE ExampleDDE Example
FILENAME biostat DDE 'Excel|book1!r1c1:r27c2'; FILENAME biostat DDE 'Excel|book1!r1c1:r27c2';
DATA NEW; DATA NEW;
INFILE biostat dlm='09'x notab dsd missover; INFILE biostat dlm='09'x notab dsd missover;
INFORMAT seqno 10. no 2.; INFORMAT seqno 10. no 2.;
INPUT seqno no; RUN;INPUT seqno no; RUN;
Note: Note:
SAS reads in the first 27 rows and 2 columns of the SAS reads in the first 27 rows and 2 columns of the spreadsheet named book1 in a open Excel file spreadsheet named book1 in a open Excel file through the Dynamic Data Exchange (DDE).through the Dynamic Data Exchange (DDE).
1818
Statements for Outputting Statements for Outputting DataData
file:file: Specifies the current output file for PUT Specifies the current output file for PUT statementsstatements
put:put: Writes lines to the SAS log, to the SAS Writes lines to the SAS log, to the SAS procedure output file, or to an external file procedure output file, or to an external file that is specified in the most recent FILE that is specified in the most recent FILE statement.statement.
Example:Example:data _null_; data _null_;
set new;set new;
file 'c:\out.csv' delimiter=',' dsd;file 'c:\out.csv' delimiter=',' dsd;
put seqno no ; put seqno no ;
run; run;
1919
ComparisonsComparisons The INFILE statement specifies the The INFILE statement specifies the input fileinput file for for
any INPUT statements in the DATA step. The FILE any INPUT statements in the DATA step. The FILE statement specifies the statement specifies the output fileoutput file for any PUT for any PUT statements in the DATA step. statements in the DATA step.
Both the FILE and INFILE statements allow you to Both the FILE and INFILE statements allow you to use options that provide SAS with additional use options that provide SAS with additional information about the external file being used.information about the external file being used.
An INFILE statement usually identifies data from an An INFILE statement usually identifies data from an external file. A DATALINES statement indicates that external file. A DATALINES statement indicates that data follow in the job stream. You can use the data follow in the job stream. You can use the INFILE statement with the file specification INFILE statement with the file specification DATALINES to take advantage of certain data-DATALINES to take advantage of certain data-reading options that effect how the INPUT reading options that effect how the INPUT statement reads in-stream data.statement reads in-stream data.
2020
Read Dates with Formatted Read Dates with Formatted InputInput
DATA Dates;DATA Dates;INPUT @1 A date11. INPUT @1 A date11. @13 B ddmmyy6.@13 B ddmmyy6. @20 C mmddyy10. @20 C mmddyy10. @31 D yymmdd8.;@31 D yymmdd8.;
duration=A-mdy(1,1,1970);duration=A-mdy(1,1,1970);FORMAT A B C D mmddyy10.; cards;FORMAT A B C D mmddyy10.; cards;13/APR/1999 130499 04-13-1999 99 04 1313/APR/1999 130499 04-13-1999 99 04 1301/JAN/1960 010160 01-01-1960 60 01 01;01/JAN/1960 010160 01-01-1960 60 01 01;RUN;RUN;Obs A B C D durationObs A B C D duration 1 04/13/1999 04/13/1999 04/13/1999 04/13/1999 106941 04/13/1999 04/13/1999 04/13/1999 04/13/1999 10694 2 01/01/1960 01/01/1960 01/01/1960 01/01/1960 -36532 01/01/1960 01/01/1960 01/01/1960 01/01/1960 -3653
2121
Procedures To Procedures To Import/Outport DataImport/Outport Data
IMPORT:IMPORT: reads data from an external data reads data from an external data source and writes it to a SAS data set.source and writes it to a SAS data set.
CPORT:CPORT: writes SAS data sets, SAS writes SAS data sets, SAS catalogs, or SAS data libraries to catalogs, or SAS data libraries to sequential file formats (transport files). sequential file formats (transport files).
CIMPORT:CIMPORT: imports a transport file that was imports a transport file that was created (exported) by the CPORT created (exported) by the CPORT procedure. It restores the transport file to procedure. It restores the transport file to its original form as a SAS catalog, SAS its original form as a SAS catalog, SAS data set, or SAS data library.data set, or SAS data library.
2222
PROC IMPORTPROC IMPORT
Syntax: Syntax: PROC IMPORT PROC IMPORT
DATAFILE="filename" | TABLE="tablename" DATAFILE="filename" | TABLE="tablename"
OUT=SAS-data-set OUT=SAS-data-set
<DBMS=identifier><REPLACE>; <DBMS=identifier><REPLACE>;
2323
PORC IMPORTPORC IMPORT
Space.txt:Space.txt:MAKE MPG WEIGHT PRICEMAKE MPG WEIGHT PRICE AMC 22 2930 4099 AMC 22 2930 4099 AMC 17 3350 4749 AMC 17 3350 4749 AMC 22 2640 3799 AMC 22 2640 3799 Buick 20 3250 4816 Buick 20 3250 4816 Buick 15 4080 7827Buick 15 4080 7827
proc import datafile="space.txt" proc import datafile="space.txt" out=mydata =mydata dbms==dlm replace; ;
getnames=yes; =yes; datarow=4; =4; run;run;
2424
Common DBMS Common DBMS SpecificationsSpecifications
IdentifierIdentifier Input Data SourceInput Data Source ExtensionExtension
ACCESSACCESS Microsoft Access Microsoft Access DatabaseDatabase
.MDB.MDB
DBFDBF dBASE filedBASE file .DBF.DBF
EXCELEXCEL EXCEL fileEXCEL file .XLS.XLS
DLMDLM delimited file (default delimited file (default delimiter is a blank)delimiter is a blank)
.*.*
CSVCSV comma-separated comma-separated filefile
.CSV.CSV
TABTAB tab-delimited filetab-delimited file .TXT.TXT
2525
SAS Programming EfficiencySAS Programming Efficiency
CPU timeCPU time I/O timeI/O time MemoryMemory Data storageData storage Programming timeProgramming time
2626
Use ELSE statement to reduce Use ELSE statement to reduce CPU timeCPU time
IF agegrp=3 THEN DO;...END;IF agegrp=3 THEN DO;...END;
IF agegrp=2 THEN DO;...END;IF agegrp=2 THEN DO;...END;
IF agegrp=1 THEN DO;...END;IF agegrp=1 THEN DO;...END;
IF agegrp=3 THEN IF agegrp=3 THEN DO;...END;DO;...END;
ELSE IF agegrp=2 THEN DO;...END;ELSE IF agegrp=2 THEN DO;...END;
ELSE IF agegrp=1 THEN DO;...END;ELSE IF agegrp=1 THEN DO;...END;
2727
Subset a SAS DatasetSubset a SAS Dataset
DATA div1; SET adults;DATA div1; SET adults;
IF division=1; RUN;IF division=1; RUN;
DATA div2; SET adults;DATA div2; SET adults;
IF division=2; RUN;IF division=2; RUN;
DATA div1 div2;DATA div1 div2;
SET adults;SET adults;
IF division=1 THEN OUTPUT div1;IF division=1 THEN OUTPUT div1;
ELSE IF division=2 THEN OUTPUT div2;ELSE IF division=2 THEN OUTPUT div2;
2828
MODIFY is Better Than SETMODIFY is Better Than SET
DATA salary;DATA salary;
SET salary;SET salary;
wages=wagesy*0.1;wages=wagesy*0.1;
DATA salary;DATA salary;
MODIFY salary;MODIFY salary;
wages=wages*0.1;wages=wages*0.1;
2929
Save Space by DROP or Save Space by DROP or KEEPKEEP
DATA new;DATA new;
SET old (KEEP=a b c); SET old (KEEP=a b c);
RUN;RUN;
DATA new;DATA new;
SET old (DROP=a);SET old (DROP=a);
RUN; RUN;
3030
Save Space by Deleting Save Space by Deleting Data SetsData Sets
DATA three;DATA three;MERGE one two;MERGE one two;BY type;BY type;RUN;RUN;
PROC DATASETS;PROC DATASETS;DELETE one two; DELETE one two; RUN;RUN;
3131
Save Space by CompressSave Space by Compress
DATA new DATA new (COMPRESS=YES);(COMPRESS=YES);SET old;SET old;
PROC SORT DATA=a OUT=b PROC SORT DATA=a OUT=b (COMPRESS=YES);(COMPRESS=YES);
PROC SUMMARY;PROC SUMMARY;VAR score;VAR score;OUTPUT OUT=SUM1 OUTPUT OUT=SUM1 (COMPRESS=YES)(COMPRESS=YES)
SUM=;SUM=;
3232
Read Only What You NeedRead Only What You Need
DATA large:DATA large:
INFILE myDATA;INFILE myDATA;
INPUT @15 type $2. @ ;INPUT @15 type $2. @ ;
INPUT @1 X $1. @2 Y $5. ;INPUT @1 X $1. @2 Y $5. ;
DATA large:DATA large:
INFILE myDATA;INFILE myDATA;
INPUT @15 type $2. @ ;INPUT @15 type $2. @ ;
IF type in ('10','11','12') THENIF type in ('10','11','12') THEN
INPUT @1 X $1. @2 Y $5.;INPUT @1 X $1. @2 Y $5.;
3333
PROC FORMAT Is Better PROC FORMAT Is Better Than Than
IF-THENIF-THENDATA new; DATA new; SET old;SET old; IF 0 LE age LE 10 THEN agegroup=0;IF 0 LE age LE 10 THEN agegroup=0;ELSE IF 10 LE age LE 20 THEN agegroup=10;ELSE IF 10 LE age LE 20 THEN agegroup=10;ELSE IF 20 LE age LE 30 THEN agegroup=20;ELSE IF 20 LE age LE 30 THEN agegroup=20;ELSE IF 30 LE age LE 40 THEN agegroup=30; ELSE IF 30 LE age LE 40 THEN agegroup=30; RUN;RUN;
PROC FORMAT;PROC FORMAT;VALUE age 0-09=0 10-19=10 20-29=20 30-39=30;VALUE age 0-09=0 10-19=10 20-29=20 30-39=30;RUN;RUN;
DATA new; DATA new; SET old; SET old; agegroup=PUT(age,age.);agegroup=PUT(age,age.);RUN;RUN;
3434
Shorten Expressions with Shorten Expressions with FunctionsFunctions
array c{10} cost1-cost10;array c{10} cost1-cost10;
tot=0;tot=0;
do I=1 to 10;do I=1 to 10;
if c{i} ne . then do;if c{i} ne . then do;
tot+c{i};tot+c{i};
end;end;
end;end;
tot=sum(of cost1-cost10);tot=sum(of cost1-cost10);
3535
IF-THEN Better Than ANDIF-THEN Better Than AND
IF status1=1 and status2=9 THEN OUTPUT;IF status1=1 and status2=9 THEN OUTPUT;
IF status1=1 THENIF status1=1 THEN
IF status2=9 THEN OUTPUT;IF status2=9 THEN OUTPUT;
3636
Use SAS Functions Use SAS Functions Whenever PossibleWhenever Possible
DATA new; SET old; DATA new; SET old;
meanxyz = (x+y+z)/3;meanxyz = (x+y+z)/3;
RUN; RUN;
DATA new; SET old;DATA new; SET old;
meanxyz = mean(x, y, z);meanxyz = mean(x, y, z);
RUN;RUN;
3737
Use RETAIN to Initialize Use RETAIN to Initialize ConstantsConstants
DATA new; SET old; DATA new; SET old; a = 5; b = 13; a = 5; b = 13; (programming statements); RUN;(programming statements); RUN;
DATA new; SET old;DATA new; SET old;retain a 5 b 13;retain a 5 b 13;(programming statements);(programming statements);RUN;RUN;
3838
Efficient SortEfficient Sort
PROC SORT;PROC SORT;BY vara varb varc vard vare; BY vara varb varc vard vare; RUN;RUN;
DATA new; SET old;DATA new; SET old;sortvar=vara||varb||varc||vard||vare;sortvar=vara||varb||varc||vard||vare; RUN;RUN;PROC SORT; PROC SORT; BY sortvar; BY sortvar; RUN;RUN;
3939
Use Arrays and MacrosUse Arrays and Macros
Using arrays and macros can save you the Using arrays and macros can save you the time of having to repeatedly type groups of time of having to repeatedly type groups of statements.statements.– Example: Convert Missing Values to 0Example: Convert Missing Values to 0
data one; input chr $ a b c; cards;data one; input chr $ a b c; cards;
xx 2 . 9 2 . 9
y . 3 .y . 3 .
z 8 . .;z 8 . .;
data two; set one; drop i;data two; set one; drop i;
array x(*) _numeric_;array x(*) _numeric_;
do i= 1 to dim(x);do i= 1 to dim(x);
if x(i) = . then x(i)=0;if x(i) = . then x(i)=0;
end; run;end; run;
4040
When When ww has many missing has many missing values.values.
DATA new; DATA new;
SET old; SET old;
wyzsum = 26 + y + z + w; wyzsum = 26 + y + z + w;
RUN;RUN;
DATA new; DATA new;
SET old;SET old;
IF x > . THEN wyzsum = 26 + y + z + w;IF x > . THEN wyzsum = 26 + y + z + w;
RUN; RUN;
4141
Put Loops With the Fewest Put Loops With the Fewest Iterations OutermostIterations Outermost
DATA new; DATA new;
SET old; SET old;
DO i = 1 TO 100; DO i = 1 TO 100;
DO j = 1 TO 10; DO j = 1 TO 10;
(programming (programming statements);statements);
END; END;
END; END;
RUN; RUN;
DATA new; DATA new; SET old; SET old; DO i = 1 TO DO i = 1 TO 1010; ; DO j = 1 TO DO j = 1 TO 100100;;(programming (programming
statements);statements);END; END; END; END; RUN; RUN;
4242
IN Better Than ORIN Better Than OR
IF status=1 OR status=5 THEN IF status=1 OR status=5 THEN newstat="single";newstat="single";
ELSE newstat="not single";ELSE newstat="not single";
IF IF status IN (1,5)status IN (1,5) THEN newstat="single"; THEN newstat="single";
ELSE newstat="not single";ELSE newstat="not single";
4343
SAS Macro SAS Macro
What can we do with Macro?What can we do with Macro?
Avoid repetitious SAS codeAvoid repetitious SAS code Create generalizable and flexible SAS codeCreate generalizable and flexible SAS code Pass information from one part of a SAS job to Pass information from one part of a SAS job to
anotheranother Conditionally execute data steps and PROCsConditionally execute data steps and PROCs Dynamically create code at execution timeDynamically create code at execution time
4444
SAS Macro FacilitySAS Macro Facility
SAS macro variableSAS macro variable SAS MacroSAS Macro Autocall Macro Facility Autocall Macro Facility Stored Compiled Macro FacilityStored Compiled Macro Facility
4545
SAS Macro DelimitersSAS Macro Delimiters
Two delimiters will trigger the macro Two delimiters will trigger the macro processor in a SAS program.processor in a SAS program.
¯o-name¯o-name This refers to a macro variable. The current This refers to a macro variable. The current
value of the variable will replace ¯o-name;value of the variable will replace ¯o-name;
%macro-name%macro-name This refers to a macro, which consists of one or This refers to a macro, which consists of one or
more complete SAS statements, or even whole data more complete SAS statements, or even whole data or proc steps.or proc steps.
4646
SAS Macro VariablesSAS Macro Variables
SAS Macro variables can be SAS Macro variables can be defined and used anywhere in a defined and used anywhere in a SAS program, except in data SAS program, except in data lines. They are independent of a lines. They are independent of a SAS dataset. SAS dataset.
Macro variables contain a single Macro variables contain a single character value that remains character value that remains constant until it is explicitly constant until it is explicitly changed.changed.
4747
SAS Macro Variables SAS Macro Variables
%LET:%LET: assign text to a macro variable; assign text to a macro variable; %LET %LET macrovarmacrovar = = valuevalue1. Macrovar is the name of a global macro variable;1. Macrovar is the name of a global macro variable;2. Value is macro variable value, which is a character string 2. Value is macro variable value, which is a character string
without quotation or macro expression.without quotation or macro expression.
%PUT: display macro variable values as text in %PUT: display macro variable values as text in the SAS log; %put _all_, %put _user_the SAS log; %put _all_, %put _user_
¯ovar: Substitute the value of a macro ¯ovar: Substitute the value of a macro variable in a program; variable in a program;
4848
SAS Macro Variables SAS Macro Variables
SAS-supplied Macro Variables:SAS-supplied Macro Variables: %put &SYSDAY; Tuesday%put &SYSDAY; Tuesday
%put &SYSDATE; 30SEP03%put &SYSDATE; 30SEP03
%put &SYSTIME; 11:02%put &SYSTIME; 11:02
%put &SYSVER; 8.2%put &SYSVER; 8.2
%put _all_%put _all_ shows shows SAS-supplied SAS-supplied
automatic and user-defined macro automatic and user-defined macro variables.variables.
4949
SAS Macro Variables SAS Macro Variables
Combine Macro Variables with Text Combine Macro Variables with Text %LET first = John; %LET first = John; %LET last = Smith; %LET last = Smith; %put &first.&last; (combine)%put &first.&last; (combine) %put &first. &last; (blank separate)%put &first. &last; (blank separate) %put Mr. &first. &last; (prefix)%put Mr. &first. &last; (prefix) %put &first. &last. HSPH; (suffix)%put &first. &last. HSPH; (suffix)output:output: JohnSmithJohnSmith John SmithJohn Smith Mr. John SmithMr. John Smith John Smith HSPH John Smith HSPH
5050
Create SAS Macro Create SAS Macro
Definition:Definition:%MACRO macro-name (parm1, parm2,…%MACRO macro-name (parm1, parm2,…
parmk);parmk);
Macro definition (&parm1,&parm2,…&parmk)Macro definition (&parm1,&parm2,…&parmk)
%MEND macro-name;%MEND macro-name;
Application:Application:%macro-name(values of parm1, parm2,%macro-name(values of parm1, parm2,
…,parmk);…,parmk);
5151
SAS Macro ExampleSAS Macro Example
Import Excel to SAS Datasets by a MacroImport Excel to SAS Datasets by a Macro%macro excelsas(in=, out=); %macro excelsas(in=, out=); proc import out=work.&outproc import out=work.&out datafile="c:\&in"datafile="c:\&in" dbms=excel2000 replace;dbms=excel2000 replace; getnames=yes; run;getnames=yes; run;%mend excelsas;%mend excelsas;
% excelsas(class1, score1) % excelsas(class1, score1) % excelsas(class2, score2) % excelsas(class2, score2)
5252
SAS System OptionsSAS System Options
System options are global instructions that affect System options are global instructions that affect the entire SAS session and control the way SAS the entire SAS session and control the way SAS performs operations. SAS system options differ performs operations. SAS system options differ from SAS data set options and statement options from SAS data set options and statement options in that once you invoke a system option, it in that once you invoke a system option, it remains in effect for all subsequent remains in effect for all subsequent datadata and and procproc steps in a SAS job, unless you specify steps in a SAS job, unless you specify them.them.
In order to view which options are available and In order to view which options are available and in effect for your SAS session, use in effect for your SAS session, use proc optionsproc options..
PROC OPTIONS; RUN;PROC OPTIONS; RUN;
5353
SAS system optionsSAS system options NOCAPS Translate quoted strings and titles to upper case? NOCAPS Translate quoted strings and titles to upper case? CENTER Center SAS output? CENTER Center SAS output? DATE Date printed in title? DATE Date printed in title? ERRORS=20 Maximum number of observations with error messages ERRORS=20 Maximum number of observations with error messages FIRSTOBS=1 First observation of each data set to be processed FIRSTOBS=1 First observation of each data set to be processed FMTERR Treat missing format or informat as an error? FMTERR Treat missing format or informat as an error? LABEL Allow procedures to use variable labels? LABEL Allow procedures to use variable labels? LINESIZE=96 Line size for printed output LINESIZE=96 Line size for printed output MISSING=. Character printed to represent numeric missing values MISSING=. Character printed to represent numeric missing values NUMBER Print page number on each page of SAS output? NUMBER Print page number on each page of SAS output? OBS=MAX Number of last observation to be processed OBS=MAX Number of last observation to be processed PAGENO=1 Resets the current page number on the print file PAGENO=1 Resets the current page number on the print file PAGESIZE=54 Number of lines printed per page of output PAGESIZE=54 Number of lines printed per page of output YEARCUTOFF=1900 Cutoff year for DATE7. informatYEARCUTOFF=1900 Cutoff year for DATE7. informat
5454
Log, output and procedure Log, output and procedure optionsoptions
centercenter controls whether SAS procedure output is centered. By default, output is controls whether SAS procedure output is centered. By default, output is centered. To specify not centered, use centered. To specify not centered, use nocenternocenter..
datedate prints the date and time to the log and output window. By default, the date prints the date and time to the log and output window. By default, the date and time is printed. To suppress the printing of the date, use and time is printed. To suppress the printing of the date, use nodatenodate..
labellabel allows SAS procedures to use labels with variables. By default, labels are allows SAS procedures to use labels with variables. By default, labels are permitted. To suppress the printing of labels, use permitted. To suppress the printing of labels, use nolabelnolabel..
notesnotes controls whether notes are printed to the SAS log. By default, notes are controls whether notes are printed to the SAS log. By default, notes are printed. To suppress the printing of notes, use printed. To suppress the printing of notes, use nonotesnonotes..
numbernumber controls whether page numbers are printed. By default, page numbers controls whether page numbers are printed. By default, page numbers are printed. To suppress the printing of page numbers, use are printed. To suppress the printing of page numbers, use nonumbernonumber..
linesize=linesize= specifies the line size (printer line width) for the SAS log and the SAS specifies the line size (printer line width) for the SAS log and the SAS procedure output file used by the procedure output file used by the datadata step and procedures. step and procedures.
pagesize=pagesize= specifies # of lines that can be printed per page of SAS output. specifies # of lines that can be printed per page of SAS output. missing=missing= specifies the character to be printed for missing numeric values. specifies the character to be printed for missing numeric values. formchar=formchar= specifies the the list of graphics characters that define table specifies the the list of graphics characters that define table
boundaries. boundaries.
Example:Example:OPTIONS NOCENTER NODATE NONOTES LINESIZE=80 MISSING=.OPTIONS NOCENTER NODATE NONOTES LINESIZE=80 MISSING=. ; ;
5555
SAS data set control optionsSAS data set control options
SAS data set control options specify how SAS SAS data set control options specify how SAS data sets are input, processed, and output. data sets are input, processed, and output.
firstobs=firstobs= causes SAS to begin reading at a specified causes SAS to begin reading at a specified observation in a data set. The default is observation in a data set. The default is firstobs=1firstobs=1..
obs=obs= specifies the last observation from a data set or the last specifies the last observation from a data set or the last record from a raw data file that SAS is to read. To return to record from a raw data file that SAS is to read. To return to using all observations in a data set use using all observations in a data set use obs=all obs=all
replacereplace specifies whether permanently stored SAS data sets specifies whether permanently stored SAS data sets are to be replaced. By default, the SAS system will over-write are to be replaced. By default, the SAS system will over-write existing SAS data sets if the SAS data set is re-specified in a existing SAS data sets if the SAS data set is re-specified in a datadata step. To suppress this option, use step. To suppress this option, use noreplacenoreplace..
Example:Example: OPTIONS OBS=100 NOREPLACE;OPTIONS OBS=100 NOREPLACE;
5656
Error handling optionsError handling options
Error handling options specify how the SAS System Error handling options specify how the SAS System reports on and recovers from error conditions. reports on and recovers from error conditions.
errors=errors= controls the maximum number of observations for which controls the maximum number of observations for which complete error messages are printed. The default maximum complete error messages are printed. The default maximum number of complete error messages is number of complete error messages is errors=20errors=20
fmterrfmterr controls whether the SAS System generates an error controls whether the SAS System generates an error message when the system cannot find a format to associate with a message when the system cannot find a format to associate with a variable. SAS will generate an ERROR message for every unknown variable. SAS will generate an ERROR message for every unknown format it encounters and will terminate the SAS job without format it encounters and will terminate the SAS job without running any following running any following datadata and and procproc steps. To read a SAS system steps. To read a SAS system data set without requiring a SAS format library, use data set without requiring a SAS format library, use nofmterr.nofmterr.
Example:Example:OPTIONS ERRORS=100 NOFMTERR; OPTIONS ERRORS=100 NOFMTERR;
5757
Using where statementUsing where statement
wherewhere statement allows us to run procedures statement allows us to run procedures on a subset records.on a subset records.
Examples:Examples:
PROC PRINT DATA=auto; PROC PRINT DATA=auto; WHERE (rep78 >= 3); WHERE (rep78 >= 3); VAR make rep78; VAR make rep78; RUN;RUN;
PROC PRINT DATA=auto; PROC PRINT DATA=auto; WHERE (rep78 <= 2) and (rep78 ^= .) ; WHERE (rep78 <= 2) and (rep78 ^= .) ; VAR make price rep78 ; VAR make price rep78 ; RUN; RUN;
5858
Summary of how missing Summary of how missing values are handled in SAS values are handled in SAS
procedures procedures As a general rule, SAS procedures that perform As a general rule, SAS procedures that perform computations handle missing data by omitting the computations handle missing data by omitting the missing values. missing values.
proc meansproc meansFor each variable, the number of non-missing For each variable, the number of non-missing values are used values are used
proc freqproc freqBy default, missing values are excluded and By default, missing values are excluded and percentages are based on the number of non-percentages are based on the number of non-missing values. If you use the missing values. If you use the missingmissing option on option on the the tablestables statement, the percentages are based statement, the percentages are based on the total number of observations (non-missing on the total number of observations (non-missing and missing) and the percentage of missing values and missing) and the percentage of missing values are reported in the table.are reported in the table.
5959
Summary of how missing Summary of how missing values are handled in SAS values are handled in SAS
proceduresprocedures proc corrproc corrBy default, correlations are computed based on By default, correlations are computed based on the number of pairs with non-missing data the number of pairs with non-missing data ((pairwise deletion of missing data)pairwise deletion of missing data). The . The nomissnomiss option can be used to request that option can be used to request that correlations be computed only for observations correlations be computed only for observations that have non-missing data for all variables on the that have non-missing data for all variables on the varvar statement ( statement (listwise deletion of missing listwise deletion of missing datadata). ).
proc regproc regIf any of the variables on the If any of the variables on the modelmodel or or varvar statement are missing, they are excluded from the statement are missing, they are excluded from the analysis (i.e., analysis (i.e., listwise deletion of missing data)listwise deletion of missing data)
6060
Summary of how missing Summary of how missing values are handled in SAS values are handled in SAS
proceduresprocedures proc glmproc glmIf you have an analysis with just one variable If you have an analysis with just one variable on the left side of the model statement (just on the left side of the model statement (just one outcome or dependent variable), one outcome or dependent variable), observations are eliminated if any of the observations are eliminated if any of the variables on the model statement are variables on the model statement are missing. Likewise, if you are performing a missing. Likewise, if you are performing a repeated measures ANOVArepeated measures ANOVA or a or a MANOVAMANOVA, then observations are eliminated , then observations are eliminated if any of the variables in the model if any of the variables in the model statement are missing. For other situations, statement are missing. For other situations, see the SAS/STAT manual about see the SAS/STAT manual about proc glmproc glm. .
6161
Missing values in Missing values in assignment statementsassignment statements
As a general rule, computations involving As a general rule, computations involving missing values yield missing values.missing values yield missing values.
2 + 2 yields 42 + 2 yields 42 + . yields .2 + . yields .
meanmean(of var1-varn):average the data for (of var1-varn):average the data for the non-missing values in a list of variables. the non-missing values in a list of variables. avg = mean(of var1-var10)avg = mean(of var1-var10)NN(of var1-varn): determine the number of (of var1-varn): determine the number of non-missing values in a list of variablesnon-missing values in a list of variables
n = N(var1, var2, var3)n = N(var1, var2, var3)
6262
Missing values in logical Missing values in logical statementsstatements
SAS treats a missing value as the SAS treats a missing value as the smallest possible value (e.g., negative smallest possible value (e.g., negative infinity) in logical statements.infinity) in logical statements.
DATA times6; DATA times6; SET times ; SET times ; if (var1 <= 1.5) then varc1 = 0; else varc1 = 1 ; if (var1 <= 1.5) then varc1 = 0; else varc1 = 1 ; RUN ; RUN ;
Output:Output:Obs id var1 varc1 Obs id var1 varc1 11 1 1.51 1.5 0 0 22 2 .2 . 0 0 33 3 2.13 2.1 1 1
6363
Subsetting DataSubsetting Data
Subsetting variables using Subsetting variables using keepkeep or or drop drop statementsstatements
Example:Example:DATA auto2; DATA auto2;
SET auto; SET auto; KEEP make mpg price; KEEP make mpg price; RUN;RUN;
DATA auto3; DATA auto3; SET auto; SET auto; DROP rep78 hdroom trunk weight length turn displ gratio DROP rep78 hdroom trunk weight length turn displ gratio
foreign; foreign; RUN;RUN;
6464
Subsetting DataSubsetting Data
Subsetting observations using Subsetting observations using ifif statements statements
Example:Example:DATA auto4; DATA auto4;
SET auto; SET auto; IF rep78 ^= . ;IF rep78 ^= . ; RUN;RUN;
DATA auto5; DATA auto5; SET auto; SET auto; IF rep78 > 3 THEN DELETE ; IF rep78 > 3 THEN DELETE ; RUN;RUN;
6565
Labeling variablesLabeling variables
Variable label: Use the Variable label: Use the labellabel statement in the data statement in the data step to assign labels to the variables.step to assign labels to the variables. You could You could also assign labels to variables in proc steps, but also assign labels to variables in proc steps, but then the labels only exist for that step.then the labels only exist for that step. When labels When labels are assigned in the data step they are available for are assigned in the data step they are available for all procedures that use that data set.all procedures that use that data set.
Example:Example: DATA auto2; DATA auto2; SET auto; SET auto; LABEL rep78 ="1978 Repair Record" mpg ="Miles Per Gallon" LABEL rep78 ="1978 Repair Record" mpg ="Miles Per Gallon"
foreign="Where Car Was Made"; foreign="Where Car Was Made"; RUN; RUN; PROC CONTENTS DATA=auto2; PROC CONTENTS DATA=auto2; RUN;RUN;
6666
Labeling variable valuesLabeling variable values
Labeling values is a two step process.Labeling values is a two step process. First, you must First, you must create the label formats with create the label formats with proc formatproc format using a using a valuevalue statement. statement. Next, you attach the label format to Next, you attach the label format to the variable with a the variable with a formatformat statement. statement. This This formatformat statement can be used in either statement can be used in either procproc or or datadata steps. steps.
Example:Example:*first create the label formats forgnf and makef;*first create the label formats forgnf and makef;PROC FORMAT; PROC FORMAT; VALUE forgnf 0="domestic" 1="foreign" ; VALUE forgnf 0="domestic" 1="foreign" ; VALUE $makef "AMC" ="American Motors" "Buick" ="Buick (GM)" "Cad." ="Cadallac VALUE $makef "AMC" ="American Motors" "Buick" ="Buick (GM)" "Cad." ="Cadallac
(GM)" "Chev." ="Cheverolet (GM)" "Datsun" ="Datsun (Nissan)"; (GM)" "Chev." ="Cheverolet (GM)" "Datsun" ="Datsun (Nissan)"; RUN;RUN;*now we link them to the variables foreign and make;*now we link them to the variables foreign and make;PROC FREQ DATA=auto2; PROC FREQ DATA=auto2; FORMAT foreign forgnf. make $makef.; FORMAT foreign forgnf. make $makef.; TABLES foreign make; RUN;TABLES foreign make; RUN;
6767
Sort dataSort data
Use Use proc sortproc sort to sort this data file. to sort this data file. Examples:Examples:PROC SORT DATA=auto ; BY foreign ; RUN ; PROC SORT DATA=auto ; BY foreign ; RUN ;
PROC SORT DATA=auto OUT=auto2 ; PROC SORT DATA=auto OUT=auto2 ; BY foreign ; RUN ; BY foreign ; RUN ;
PROC SORT DATA=auto OUT=auto3; PROC SORT DATA=auto OUT=auto3; BY descending foreign ; RUN ; BY descending foreign ; RUN ;
PROC SORT DATA=auto OUT=auto2 noduplicates;PROC SORT DATA=auto OUT=auto2 noduplicates;BY foreign ; RUN ; BY foreign ; RUN ;
6868
Making and using Making and using permanent SAS data filespermanent SAS data files
Use a Use a libnamelibname statement. statement. libname diss 'c:\dissertation\'; libname diss 'c:\dissertation\'; data diss.salary; data diss.salary; input sal1996-sal2000 ; input sal1996-sal2000 ; cards; cards; 14000 16500 18000 22000 29000 14000 16500 18000 22000 29000 ; ; run;run;
specify the name of the data file by directly specify the name of the data file by directly specifying the path name of the file specifying the path name of the file
data 'c:\dissertation\salarylong'; data 'c:\dissertation\salarylong'; input Salary1996-Salary2000 ; input Salary1996-Salary2000 ; cards; cards; 14000 16500 18000 22000 29000 14000 16500 18000 22000 29000 ; ; run;run;
6969
Merge data filesMerge data files
One-to-one merge: there are three steps to One-to-one merge: there are three steps to match merge two data files dads and match merge two data files dads and faminc on the same variable famid.faminc on the same variable famid.
1.1. Use Use proc sortproc sort to sort to sort dadsdads on on famidfamid and save that file (we will call it and save that file (we will call it dads2dads2) )
PROC SORT DATA=dads OUT=dads2; BY famid; RUN; PROC SORT DATA=dads OUT=dads2; BY famid; RUN;
• Use Use proc sortproc sort to sort to sort famincfaminc on on famidfamid and save that file (we will call it and save that file (we will call it faminc2faminc2) )
PROC SORT DATA=faminc OUT=faminc2; BY famid; RUN; PROC SORT DATA=faminc OUT=faminc2; BY famid; RUN;
• merge the merge the dads2dads2 and and faminc2faminc2 files files based on based on famidfamid
DATA dadfam ; MERGE dads2 faminc2; BY famid; RUN: DATA dadfam ; MERGE dads2 faminc2; BY famid; RUN:
7070
Merge data filesMerge data files
One-to-many merge: there are three steps One-to-many merge: there are three steps to match merge two data files dads and to match merge two data files dads and kids on the same variable famid.kids on the same variable famid.
1.1. Use Use proc sortproc sort to sort to sort dadsdads on on famidfamid and save that file (we will call it and save that file (we will call it dads2dads2) )
PROC SORT DATA=dads OUT=dads2; BY famid; RUN; PROC SORT DATA=dads OUT=dads2; BY famid; RUN;
• Use Use proc sortproc sort to sort to sort kidskids on on famidfamid and and save that file (we will call it save that file (we will call it kid2kid2) )
PROC SORT DATA=kids OUT=kids2; BY famid; RUN; PROC SORT DATA=kids OUT=kids2; BY famid; RUN;
• merge the merge the dads2dads2 and and faminc2faminc2 files files based on based on famidfamid
DATA dadkid; MERGE dads2 kids2; BY famid; RUN: DATA dadkid; MERGE dads2 kids2; BY famid; RUN:
7171
Merge data files: mismatchMerge data files: mismatch
Mismatching records in one-to-one merge: Mismatching records in one-to-one merge: use the use the inin option to create a 0/1 variable option to create a 0/1 variable DATA merge121; DATA merge121; MERGE dads(IN=fromdadx) faminc(IN=fromfamx); MERGE dads(IN=fromdadx) faminc(IN=fromfamx); BY famid; BY famid; fromdad = fromdadx; fromdad = fromdadx; fromfam = fromfamx; fromfam = fromfamx; RUN;RUN;
Variables with the same name, but Variables with the same name, but different information: rename variablesdifferent information: rename variablesDATA merge121; DATA merge121; MERGE faminc(RENAME=(inc96=faminc96 inc97=faminc97 MERGE faminc(RENAME=(inc96=faminc96 inc97=faminc97
inc98=faminc98)) dads(RENAME=(inc98=dadinc98)); inc98=faminc98)) dads(RENAME=(inc98=dadinc98)); BY famid; BY famid; RUN; RUN;
7272
Concatenating data files in Concatenating data files in SASSAS
Use Use setset to stack data files to stack data filesDATA dadmom; SET dads moms; RUN;DATA dadmom; SET dads moms; RUN;
Use rename to stack two data files with different Use rename to stack two data files with different variable names for the same thingvariable names for the same thingDATA momdad; DATA momdad; SET dads(RENAME=(dadinc=inc)) SET dads(RENAME=(dadinc=inc))
moms(RENAME=(mominc=inc));moms(RENAME=(mominc=inc));RUN; RUN;
Two data files with different lengths Two data files with different lengths for variables for variables of the same name of the same name DATA momdad; DATA momdad; LENGTH name $ 4; LENGTH name $ 4; SET dads moms; SET dads moms; RUN; RUN;
7373
Concatenating data files in Concatenating data files in SASSAS
The two data files have variables with the The two data files have variables with the same name but different codessame name but different codes
dadsdads momsmomsfamid name inc fulltime famid name inc fulltime famid name inc fulltime; famid name inc fulltime; 1 Bill 30000 1 1 Bess 15000 N 1 Bill 30000 1 1 Bess 15000 N 2 Art 22000 0 2 Amy 18000 N 2 Art 22000 0 2 Amy 18000 N 3 Paul 25000 3 3 Pat 50000 Y; 3 Paul 25000 3 3 Pat 50000 Y;
DATA dads; SET dads; full=fulltime; DROP fulltime;RUN;DATA dads; SET dads; full=fulltime; DROP fulltime;RUN;
DATA moms; SET moms; DATA moms; SET moms; IF fulltime="Y" THEN full=1; IF fulltime="N" THEN full=0; IF fulltime="Y" THEN full=1; IF fulltime="N" THEN full=0; DROP fulltime;RUN;DROP fulltime;RUN; DATA momdad; SET dads moms;RUN;DATA momdad; SET dads moms;RUN;
7474
SAS Macro SAS Macro
What can we do with Macro?What can we do with Macro?
Avoid repetitious SAS codeAvoid repetitious SAS code Create generalizable and flexible SAS codeCreate generalizable and flexible SAS code Pass information from one part of a SAS job to Pass information from one part of a SAS job to
anotheranother Conditionally execute data steps and PROCsConditionally execute data steps and PROCs Dynamically create code at execution timeDynamically create code at execution time
7575
SAS Macro FacilitySAS Macro Facility
SAS macro variableSAS macro variable SAS MacroSAS Macro Autocall Macro Facility Autocall Macro Facility Stored Compiled Macro FacilityStored Compiled Macro Facility
7676
SAS Macro DelimitersSAS Macro Delimiters
Two delimiters will trigger the macro Two delimiters will trigger the macro processor in a SAS program.processor in a SAS program.
¯o-name¯o-name This refers to a macro variable. The current This refers to a macro variable. The current
value of the variable will replace ¯o-name;value of the variable will replace ¯o-name;
%macro-name%macro-name This refers to a macro, which consists of one or This refers to a macro, which consists of one or
more complete SAS statements, or even whole data more complete SAS statements, or even whole data or proc steps.or proc steps.
7777
SAS Macro VariablesSAS Macro Variables
SAS Macro variables can be defined SAS Macro variables can be defined and used anywhere in a SAS and used anywhere in a SAS program, except in data lines. They program, except in data lines. They are independent of a SAS dataset. are independent of a SAS dataset.
Macro variables contain a single Macro variables contain a single character value that remains character value that remains constant until it is explicitly constant until it is explicitly changed.changed.
To record the SAS macro use To record the SAS macro use options macro;options macro;
7878
SAS Macro Variables SAS Macro Variables
%LET:%LET: assign text to a macro variable; assign text to a macro variable; %LET %LET macrovarmacrovar = = valuevalue1. Macrovar is the name of a global macro variable;1. Macrovar is the name of a global macro variable;2. Value is macro variable value, which is a character string 2. Value is macro variable value, which is a character string
without quotation or macro expression.without quotation or macro expression.
%PUT:%PUT: display macro variable values as text in display macro variable values as text in the SAS log; the SAS log; %put _all_, %put _user_%put _all_, %put _user_
¯ovar:¯ovar: Substitute the value of a macro Substitute the value of a macro variable in a program; variable in a program;
7979
SAS Macro Variables SAS Macro Variables
SAS-supplied Macro Variables:SAS-supplied Macro Variables: %put &SYSDAY; Tuesday%put &SYSDAY; Tuesday
%put &SYSDATE; 30SEP03%put &SYSDATE; 30SEP03
%put &SYSTIME; 11:02%put &SYSTIME; 11:02
%put &SYSVER; 8.2%put &SYSVER; 8.2
%put _all_%put _all_ shows shows SAS-supplied SAS-supplied
automatic and user-defined macro automatic and user-defined macro variables.variables.
8080
SAS Macro Variables SAS Macro Variables
Combine Macro Variables with Text Combine Macro Variables with Text %LET first = John; %LET first = John; %LET last = Smith; %LET last = Smith; %put &first.&last; (combine)%put &first.&last; (combine) %put &first. &last; (blank separate)%put &first. &last; (blank separate) %put Mr. &first. &last; (prefix)%put Mr. &first. &last; (prefix) %put &first. &last. HSPH; (suffix)%put &first. &last. HSPH; (suffix)output:output: JohnSmithJohnSmith John SmithJohn Smith Mr. John SmithMr. John Smith John Smith HSPH John Smith HSPH
8181
Create SAS Macro Create SAS Macro
Definition:Definition:%MACRO macro-name (parm1, parm2,…%MACRO macro-name (parm1, parm2,…
parmk);parmk);
Macro definition (&parm1,&parm2,…&parmk)Macro definition (&parm1,&parm2,…&parmk)
%MEND macro-name;%MEND macro-name;
Application:Application:%macro-name(values of parm1, parm2,%macro-name(values of parm1, parm2,
…,parmk);…,parmk);
8282
SAS Macro ExampleSAS Macro Example
Import Excel to SAS Datasets by a MacroImport Excel to SAS Datasets by a Macro%macro excelsas(in,out); %macro excelsas(in,out); proc import out=work.&outproc import out=work.&out datafile="c:\&in"datafile="c:\&in" dbms=excel2000 replace;dbms=excel2000 replace; getnames=yes; run;getnames=yes; run;%mend excelsas;%mend excelsas;
% excelsas(class1, score1) % excelsas(class1, score1) % excelsas(class2, score2) % excelsas(class2, score2)
8383
SAS Macro ExampleSAS Macro Example
Use proc means by a MacroUse proc means by a Macro%macro auto(var1, var2); %macro auto(var1, var2); proc sort data=auto;proc sort data=auto;by &var2;by &var2;run;run;
proc means data=auto;proc means data=auto;var &var1;var &var1;by &var2;by &var2;run;run;%mend auto;%mend auto;
%auto(price, rep78) ;%auto(price, rep78) ;%auto(price, foreign); %auto(price, foreign);
8484
Inclass practiceInclass practice
Use the auto data to do the followingUse the auto data to do the following check missing values for each variablecheck missing values for each variable create a new variable model (first part create a new variable model (first part
of make)of make) get means/frequencies for each get means/frequencies for each
variable by modelvariable by model create 5 data files with 1-5 repairs create 5 data files with 1-5 repairs
using macrousing macro