Tag Archives: Data set

SAS Cheat Sheet – Part 4

INFORMATS

Code Description
<w.d> Reads standard numeric data
<datew.> Reads date values (ddmmmyy)/td>;
<$w.> Reads standard character data
<$VARYINGw> Reads character data of varying length

SAS represents a date internally as the number of days
between January 1, 1960 and the specified date. Dates
before 1960 are represented with negative numbers. A
SAS date value is a numeric variable.

SAS DateValue Calendar DateValue
0 January 1, 1960
30 January 31, 1960
366 January 1, 1961
-365 January 1, 1959

How do you use an InFormat to create a SAS date?

If the raw fields are aligned in columns, use formatted input and specify the informat.
e.g. input @1 visitdt yymmdd8.;

With list input, use a separate INFORMAT statement or a modifier.
informat visitdtyymmdd8.;
input visitdt;
input visitdt : yymmdd8.;

How do you refer to a particular date in SAS?

To create a SAS date constant, write the date enclosed in single or double quotes, followed by a D.
e.g. age = ’20-MAR-2012’d – incdt;
where visitdt lt “1may12”D;

How do you work with SAS date values?

When a variable is a SAS date value, you can easily apply operations such as addition and subtraction. To find the number of days between two dates, simply subtract the two SAS date variables.
e.g. daysbtwn = visitdt1 – visitdt2;

Comparison operators can also be used.
if visitdt1 <; visitdt2 then do;

Advertisements

SAS Cheat Sheet – Part 6

The macro facility is a tool you can use in your programming./*

Macro Language

%DO macro-var=start_value %TO end_value

%DO %WHILE (expression); /*Executes a section of a macro repetitively while a condition is true*/

%DO %UNTIL (expression); /*Executes a section of a macro repetitively until a condition is true*/

Macro variables can be stored in either the global symbol table or in a local symbol table

%GLOBAL macro-variable(s); /*Creates macro variables that are available during the execution of an entire SAS session*/

%IF expression %THEN action; <%ELSE action;> /Conditionally process a portion of a macro*/

%LENGTH (character string | text expression) /*Returns the length of a string*/

The name assigned to a macro variable must be a valid SAS name

%LET macro-variable =<value>; /*Creates a macro variable and assigns it a value*/

%MACRO mname (<pp1><…,ppn><kp1=value<..<kpn=v>);/*Begins a macro definition*/

%MEND <macro-name>; /*Ends a macro definition*/

%SCAN(argument,n<,delimiters>)/*Search for a word that is specified by its position in a string*/

%SUBSTR (argument,position<,length>)/*Produce a substring of a character string*/

%UPCASE (character string | text expression) /*Convert values to uppercase*/

Macro variable values are text values

Macro Quoting

%QUOTE | %NRQUOTE and %BQUOTE | %NRBQUOTE /*Mask special characters and mnemonic operators in a resolved value at macro execution */

%STR | %NRSTR /*Mask special characters and mnemonic operators in constant text at macro compilation */

%SUPERQ /*Masks special characters/mnemonic operators at macro execution but prevents further resolution of the value*/

Example:

*----------------------------------------------------------------*//* This macro will produce summary statistics ----------------   */

%Macro safety1;
%odscmd (start,portnum=11,rptname=AEActivityReport);
Data _null_;
thedate=today();
fstdate = mdy(month(thedate), 1, year(thedate));
lstdate = intnx(‘month’, fstdate, 1)-1;
call symput (‘strtdate’, put(fstdate, date9.));
call symput (‘stpdate’, put(thedate, date9.));
run;

%put strtdate: &strtdate;
%put stpdate: &stpdate;
* more code goes here;

/*----------------------------------------------------------------*/

Concatenating SAS data sets with the APPEND procedure

The APPEND procedure is an efficient method for concatenating observations from a smaller data set to a larger data set. The BASE= data set option is reserved for the larger of the two data sets with the DATA= option for the smaller data set. Essentially, the APPEND procedure avoids reading any observations in the BASE= data set by positioning the record pointer at the end of the BASE= data set.

If no additional processing is necessary, using PROC APPEND or the APPEND statement in PROC DATASETS is more efficient than using a DATA step to concatenate data sets.

Each observation from the smaller data set is then applied one at a time to the end of the BASE= data set. In the next example, the BASE= data set identifies a larger data set called MASTER and the DATA= data set identifies the smaller SOURCEDATA data set.

When one or more variables in the input data set (DATA=) are not present in the BASE= data set, an optional FORCE option can be specified as an option with the PROC APPEND statement to prevent a syntax error

PROC APPEND
BASE=master
DATA=sourcedata;
RUN;

WARNING: Variable charvar has different lengths on BASE and DATA files (BASE 25 DATA 30).
ERROR: No appending done because of anomalies listed above. Use FORCE option to append these files.

As the LOG message suggest, no appending is done and use of the FORCE option is necessary to append the input files. The consequent required syntax to correct this issue would look like this:

PROC APPEND
BASE=master
DATA=sourcedata force;
RUN;

WARNING: Variable charvar has different lengths on BASE and DATA files (BASE 25 DATA 30).
NOTE: FORCE is specified, so dropping/truncating will occur.

When two or more data sets need to be concatenated, multiple APPEND procedures are issued. In the next example, two separate PROC APPEND steps are specified to concatenate the two smaller data sets (mysasdata1 and mysasdata2) at the end of the larger BASE= data set.

PROC APPEND
BASE=master
DATA=mysasdata1;
RUN;

PROC APPEND
BASE=master
DATA=mysasdata2;
RUN;

You cannot use PROC APPEND to add observations to a SAS data set in a sequential library

Reference: SAS 9.2 Language Reference Concepts 2nd edition