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" and apply the conditions in dataset temp.
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" and apply the conditions in dataset temp.
Use columns VAR, SCORE and RANK of dataset TEMP2 to create the following conditions and apply them in dataset TEMP-
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
Form a new Character variable using the following conditions :
Solution : CALL EXECUTE
Example 3 : Call a Macro
Data temp;
input x;
cards;
5
10
15
20
25
30
;
run;
Data temp2;Solution : CALL EXECUTE
input var $ Score rank Section $;
cards;
x 30 3 C
x 20 2 B
x 10 1 A
;
run;
Data _null_;Note - All static components should in single or double quotes and variable components should not be in quotes.
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;
![]() |
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_;Example 2 : Print Multiple Datasets
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;
data _null_ ;The above program prints 3 datasets - temp temp2 output.
input mydata $char50. ;
call execute
("proc print" || " data = " || strip(mydata) || ";" || "run ;");
cards;
temp
temp2
output
run;
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_;Example 4 : Dynamically Call Macro
call execute ('%mymacro(6)');run;
%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;
Fantastic tutorials and blog. I am preparing for Base SAS certification exam so I am learning different topics from your blog. I was exhausted when I found. This is very completed and amazing.
ReplyDeleteThank you for your kind words. Wish you all the best for your Base SAS certification exam.
Deletewhat is the purpose for the call execute?
ReplyDeleteI was thinking of making video tutorials with simple examples. Your blog is really great in the reading format. Simple examples are the key to quickly understand concepts. Good luck. Thank you.
ReplyDeleteCongratulations, and please keep on going. Your blog is really usefull
ReplyDeleteCongratulations, and please keep on going. Your blog is really usefull
ReplyDeleteWhat is the use of || in example 2 ie "proc print" || " data
ReplyDeleteCan't we use proc print data=