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.

 

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
  • 1 reply
  • 613 views
  • 0 likes
  • 2 in conversation