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

Hi,

 

I've been trying to run a do loop for a proc sql statement using the below code

%macro create_tables(start,end);

%do i = &start. %to &end.;

proc sql;
create table a_bal as select
a.*,
b.a_balance%sysevalf(&i.)
from accounts a
left join cashflows b
on a.account_no = b.account_no;
quit;
%end;

%mend;

%create_tables(39,134);

 

But the table output only contains the first column and the last iteration of the loop i.e. a_balance134

 

Any idea how to get this table to have all the a_balance fields from 39-134?

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You always create the same table:

create table a_bal as select

so you overwrite the result in every iteration of the macro do loop.

You seem to have a data design problem (wide dataset structure), which is solved best by using proc transpose:

proc transpose data=cashflows out=cashflows_long;
by account_no;
var a_balance39-a_balance134;
run;

Then join over account_no.

 

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User

You always create the same table:

create table a_bal as select

so you overwrite the result in every iteration of the macro do loop.

You seem to have a data design problem (wide dataset structure), which is solved best by using proc transpose:

proc transpose data=cashflows out=cashflows_long;
by account_no;
var a_balance39-a_balance134;
run;

Then join over account_no.

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1 reply
  • 291 views
  • 0 likes
  • 2 in conversation