Avoiding Unnecessary DATA Steps — 2 - SAS Programming

In the last program,it was very obvious that the DATA step was unnecessary. Sometimes, it is a bit more subtle.Examine this example:

Example – INEFFICIENT

DATA ONE;
INPUT ID AGE HEIGHT WEIGHT;
DATALINES;
1 23 68 155
2 45 77 200
;
PROC MEANS N MEAN STD DATA=ONE;
VAR AGE HEIGHT WEIGHT;
RUN;
DATA EXTRA;
SET ONE;
LENGTH AGEGRP $ 8?
IF 0 LE AGE LE 20 THEN AGEGRP='0 TO 20';
ELSE IF 21 LE AGE LE 40 THEN AGEGRP='21 TO 40';
ELSE IF AGE GT 40 THEN AGEGRP='>4Q*;
RUN?
PROC FREQ DATA=EXTRA;
TABLES AGEGRP;
RUN;

Now examine the following program which eliminates the entire EXTRA data set and creates the variable AGEGRP at the same time the original data set is constructed.

Example – EFFICIENT

DATA ONE;
INPUT ID AGE HEIGHT WEIGHT;
LENGTH AGEGRP $ 8;
IF 0 LE AGE LE 20 THEN AGEGRP='0 TO 20';
ELSE IF 21 LE AGE LE 40 THEN AGEGRP='2X TO 40';
ELSE IF AGE GT 40 THEN AGEGRP='>40';
DATALINES;
1 23 68 155
2 45 77 200
*•
*
PROC MEANS N MEAN STD DATA=ONE;
VAR AGE HEIGHT WEIGHT;
RUN;
PROC FREQ;
TABLES AGEGRP;
RUN;

Even though our conceptions of the processes that need to be performed are often like the inefficient example (that is step-wise),if you plan ahead, you can usually avoid unnecessary and inefficient operations. In this case, you realized that the variable AGEGRP should be created at the same time the original data set is built.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAS Programming Topics