sasinterviewsquestion

Just another WordPress.com site

SAS Tips n Tricks

For Free Programming Workshops http://www.sastrainingdelhi.in
mob: 9990835485

1. Avoiding unnecessary sorting.

Sometimes we sort SAS data sets even when they are already sorted, Presorted option(in the 9.2 version of SAS) with Proc Sort will verify the sorting order and then only will sort the data set, if required.

Syntax:

proc sort data=mydata presorted;
by abc;
run;

This will reduce the time in sorting by removing the unnecessary
sorting of the data.

2. Checking you code for syntax error without actually executing your code
My datastep is reading external file with more than 2 lakhs records.I am not sure whether the syntax of my datastep is correct
or not and want to check my syntax before actually reading the extenal file.

Add the “cancel” keyword before the run statement and submit the code.
If the code contains error, it will show you syntax error message in sas log otherwise it will show you :
NOTE:Data Step not executed at user’s request.

data testing;
infile ‘my external file’;
input x y z;
run cancel;

3. Assign a value to a constant only once.

data test1;
infile “my ext file”;
input var1-var3;
x=5;
More Efficient:
data test1;
retain x 5;
infile “my ext file”;
input var1-var3;
SAS assigns values to variables in a retain statement only once. In contrast, SAS executes
assignment statements during each iteration of the DATA step.

4. Implicit conversions takes more CPU time than explicit conversions

DATA weight;
weight_kg = “75”;
weigth_st = 0.157473 * weight_kg;
RUN;

Message in SAS log:
NOTE: Character values have been converted to numeric values at the places given by:

To avoid the note being written to the log, you can explicitly perform the character to numeric conversion.
Efficient Way(in terms of CPU saving)
DATA weight;
weight_kg = “75”;
weigth_st = 0.157473 * INPUT(weight_kg,BEST.);
RUN;

Similar is the case with numeric to character conversion.

5. Working with proc transpose
proc sort data=have out=need;
by idnum date;
run;

proc transpose data=need out=want (drop=_:) prefix=var1_Qtr;
by idnum;
id date;
var var1;
format date Qtr1.;
run;

Efficient way :

proc sort data=have (keep=idnum date var1) out=_temp noequals;
by idnum date;
run;

proc transpose data=_temp (keep=idnum date var1) out=want (drop=_:)
prefix=var1_Qtr;
by idnum;
id date;
var var1;
format date Qtr1.;
run;

proc delete data=work._temp;
run;

6. USE THE SPECIFIC PROCEDURES FOR THE JOB

Using the specialized procedures, in place of the data step, will improve the efficiency in most of the cases.

DATA myds;
SET myds1 myds2;
RUN;

Efficient code for data appending:

PROC APPEND BASE=myds1 APPEND=myds2;
RUN;

7. USE SAS FUNCTIONS INSTEAD OF EXPRESSIONS

The SAS functions have been designed to perform the specific task and are more faster and sophisticated
that the raw expressions. For instance, if we consider the following code, with a simple sum operation:

DATA outdata;
SET inpdata;
Total = Salary + Incentive;
Run;

In case of above sas stetement, SAS processing will take time to record the missing value and also assigns a missing value for total for that particular record. At the same time, with a “SUM” function, it will execute faster and will perform summation of the non-missing values.

DATA outdata;
SET inpdata;
Total = sum(Salary , Incentive);
RUN;

Leave a comment