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!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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