Hi I want to create a loop for the following code so I can output a range of names...
data _null_;
do i = 1 to 2;
if i = 1 then class = "bank";
if i = 2 then class = "bonds";
end;
summary = cats(class,"_summary");
call symputx('summary',summary);
Current = cats(class,"_curr");
call symputx('current',current);
run;
%put &summary.;
%put ¤t.;
But right now my output looks like this
bond_summary
bond_curr
What I dont understand is why
1) my first i =1 clas = bank didnt appear?
2) why is an "s" missing in my "bond"
And then if the above code is run successfully I want to incorporate the following proc sql code but I dont know where to stick the code in the loop.
proc sql;
create table &summary. as
select Market_value
from ¤t.;
quit;
Many thanks for helping!
There are many ways to solve this type of problem, one way is to generate code and then execute the generated code.
Example:
/* Generate code */
filename pgm temp;
data _null_;
file pgm;
length class summary current $32;
do i = 1 to 2;
if i = 1 then class = "bank";
else if i = 2 then class = "bonds";
summary = catt(class,'_summary');
current = catt(class,'_curr');
put 'proc sql noprint;'
/ ' create table ' summary 'as'
/ ' select Market_Value'
/ ' from ' current ';'
/ 'quit;'
;
end;
run;
/* Excute the generated code */
%inc pgm / source2;
filename pgm clear;
Then code get written to a temporary file in the data step and then included using the %inc statement.
The / Source2 option shows the included code in the log.
Hello,
1. To see i can class variable output, you have to add put statement.
2. As per loop , when i=1 then SAS automatically assign length as per first declaration . To avoid truncation you have to use LENGTH statement .
data _null_;
length class $5.;
do i=1 to 2;
if i=1 then
class="bank";
if i=2 then
class="bonds";
put i= class=;
end;
summary=cats(class, "_summary");
call symputx('summary', summary);
Current=cats(class, "_curr");
call symputx('current', current);
run;
%put &summary.;
%put ¤t.;
There are many ways to solve this type of problem, one way is to generate code and then execute the generated code.
Example:
/* Generate code */
filename pgm temp;
data _null_;
file pgm;
length class summary current $32;
do i = 1 to 2;
if i = 1 then class = "bank";
else if i = 2 then class = "bonds";
summary = catt(class,'_summary');
current = catt(class,'_curr');
put 'proc sql noprint;'
/ ' create table ' summary 'as'
/ ' select Market_Value'
/ ' from ' current ';'
/ 'quit;'
;
end;
run;
/* Excute the generated code */
%inc pgm / source2;
filename pgm clear;
Then code get written to a temporary file in the data step and then included using the %inc statement.
The / Source2 option shows the included code in the log.
Great! Works like a charm! Many thanks!
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.