Quantcast
Channel: ListenData
Viewing all articles
Browse latest Browse all 425

SAS : Call Execute Made Easy

$
0
0
This tutorial explains how to use data step to interact with the SAS macro facility.

Example 1
Suppose you have two data sets named "temp" and "temp2". You are asked to form a group based on the logical conditions given in the other dataset "temp2".

Use columns SCORE and RANK of dataset TEMP2 and apply the following conditions -
if x less than or equal to 30 then Groups=3;
if x less than or equal to 20 then Groups=2;
if x less than or equal to 10 then Groups=1;

Create sample data sets
Data temp;
input x;
cards;
5
10
15
20
25
30
;
run;
Data temp2;
input var $ Score rank Section $;
cards;
x 30 3 C
x 20 2 B
x 10 1 A
;
run;
Solution : CALL EXECUTE
Data _null_;
set temp2 end=last;
if _n_=1 then call execute ('Data output; set temp;');
call execute ("if " ||strip(Var)|| " LE " ||strip(Score) || " then " || "Groups" || "=" ||strip(rank) ||";");
if last then call execute (' run;');
run;
Note - All static components should in single or double quotes  and variable components should not be in quotes.
Log : CALL EXECUTE

Form a new Character variable using the following conditions :
If x LE 30 then Class="C";
If x LE 20 then Class="B";
If x LE 10 then Class="A";

Solution : CALL EXECUTE 
Data _null_;
set temp2 end=last;
if _n_=1 then call execute ('Data output; set temp; length Class $10.;');
call execute ("if " ||strip(Var)|| " LE " ||strip(Score) || " then " || "Class" ||  "=" || """" || strip(Section) ||""""|| ";");
if last then call execute (' run;');
run;
Example 2 : Print Multiple Datasets
data _null_ ;
input mydata $char50. ;
call execute
("proc print" || " data = " || strip(mydata) || ";" || "run ;");
cards;
temp
temp2
output
run;
The above program prints 3 datasets - temp temp2 output.

Example 3 : Call a Macro
%macro mymacro(k);
data want;
set temp;
%do i = 1 %to &k;
if _N_ = &i then y = %eval(&i.* 10);
%end;
run;
%mend;
data _null_;
call execute ('%mymacro(6)');run;
 Example 4 : Dynamically Call Macro
%macro mymacro(i,j);
%put first = &i. second = &j.;
%mend;
DATA example;
input x y $32. ;
datalines;
1 temp
2 temp2
;
run;
data _null_;
set example;
call execute('%MyMacro('||x||','||y||')');
run; 

Viewing all articles
Browse latest Browse all 425

Trending Articles