BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
irisG
Calcite | Level 5

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 &current.;

 

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 &current.;

quit;

 

Many thanks for helping!

1 ACCEPTED SOLUTION

Accepted Solutions
MichaelLarsen
SAS Employee

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.

View solution in original post

4 REPLIES 4
singhsahab
Lapis Lazuli | Level 10

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 &current.;
MichaelLarsen
SAS Employee

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.

irisG
Calcite | Level 5

Great! Works like a charm! Many thanks!

s_lassen
Meteorite | Level 14
If it works like a charm, you should mark it as the solution!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1366 views
  • 0 likes
  • 4 in conversation