21 March 2014

Resolving Problems in Productive SAS Programming

SAS is statistical analysis software that is used for analysis of the data. During analysis of the data, programmer need to create list of certain variables in a dataset, labelling the list of variables in a dataset, and sometimes during generation of reports running the same procedures through a series of datasets. To improve productivity of coding, programmer can use macros and loops within the programming code, which avoids typing the same procedure number of times.

Using series of variables in a dataset: Most of the data in the dataset contains series of variable names with uncommon prefix, such as VAR1_LINE1 to VAR50_LINE1, VAR1_LINE2 to VAR50_LINE2 and VAR1_LINE50 to VAR50_LINE50. If in certain task of programming, programmer wants to calculate the mean values for a series of variables like VAR1_LINE1 to VAR50_LINE1 programmer faces a difficulty in producing the variables in the VAR statement of the SAS programming. Here is the solution for such type of repetitive variables.

Solution: The first solution is he/she can list these variables one by one in the VAR statement for applying analysis which consumes lot of programming time and also programmer may perform unknown errors during typing the variables. The second solution is by using macros and loops in the code and making the productive programming easy to generate the list of repetition variables for analysis. The following code is the better solution for analysis rather than specifying each variable in the VAR statement.

%MACRO Do_Vars;
%Do i = 1 %To 50;
Var&i._line1
%END;
%MEND Do_Vars;


Explanation: This macro code will generate a macro variable Do_Vars, in which repetition variables are generated with the help of, do loop. This programming avoids the time-consuming process of typing all the variables one by one and the macro variable that is generated can be used in VAR statement to invoke all the variables in the VAR statement. Here is an example of invoking the macro variable name in VAR statement.

VAR   %Do_Vars;

This above code will generate complete VAR statement, internally invoking each variable in the VAR statement, such as:

VAR   Var1_Line1   Var2_Line1 ... Var50_Line1;

Where these variables are continuously generated by macro variable that is present in the VAR statement, where this VAR statement utilizes these variables for generating average values.

Running same procedure against a series of datasets:
In the previous examples, macros and loops will generate a set of variables in a datasets. Similarly to generate a series of dataset or process steps programmer can use the programming code of macros and do loop to create process steps.

For example, there are series of SAS datasets like from SAMPLE1 to SAMPLE10 that which contains student scores and programmer has to calculate the average score for each of 10 sample datasets. So the following program will do the process of calculating the average score.

%MACRO Doing_MEAN;
%DO i = 1 %TO 10;
PROC MEANS DATA=Sample&i;
VAR SCORE;
TITLE "Average math score of Sample &i";
RUN;
%END;
%MEND Doing_MEAN;


Explanation: This macro named Doing_mean will be able to run in a loop of 10, where internally doing a mean procedure for each sample dataset.

DO_MEAN macro variable internally invokes each procedure at a time, for 10 data sets:

PROC MEANS DATA=Sample1;
VAR SCORE;
TITLE "Average math score of Sample 1";
RUN;

Explanation: So each and every loop will generate a mean procedure, where taking the score variable in the VAR statement and generating the average for each and every sample dataset. Where programmer can generate all the procedure steps at single time using macros and do loops.

Conclusion: The main purpose of using productive programming code is to possibly avoid typing the repetitive code during programming. Therefore the program becomes more reusable.

Clinnovo is a clinical innovation company. It is pioneer CRO industry in India. Clinnovo offers professional clinical research course , clinical data management course , SAS Courses and imaging training. Clinnovo has been serving different bio-pharma industries across the world with excellence and high quality. For more information contact at +91 9912868928, 040 64635501

No comments:

Post a Comment