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

data l;
input id$;
cards;
d
run;


%let a=5;

%macro tt;

data l2;
set l;
%do i= 1 %to &a;
t=id||"&i.";
t=compress(t,'');
run;
%end;

%mend;

%tt;

proc print data=l2;
run;

Tryed in this way but no working

I want to create the observations bases on macro variable a

output
id1
id2
id3
id4
id5

1 ACCEPTED SOLUTION

Accepted Solutions
Keith
Obsidian | Level 7

You have a couple of errors in your code.

  • You need an Output statement within the loop, otherwise it will only output the last iteration (the previous ones are overwritten each time)
  • The %end should come before the Run statement

You can also use the CATS function to concatenate the data and remove unwanted blanks, which will save using COMPRESS separately.  There's some slight confusion as to what you want to show in your output, should it be id1, id2 etc or d1, d2 etc ?  To achieve the former, put quotes around id in the concatenate function, otherwise leave it as is and it will take the value of the column id.  Here is the code I think you want, I've added a length statement to prevent the default $200.  You don't really need the macro either, just use a normal loop within the datastep.

%let a=5;

data l2;

length id $6;

set l;

do i= 1 to &a;

t=cats(id,i);

output;

end;

run;

proc print data=l2;

run;

Message was edited by: Keith Timms Obviously the length statement should against column t, not id.

View solution in original post

2 REPLIES 2
Keith
Obsidian | Level 7

You have a couple of errors in your code.

  • You need an Output statement within the loop, otherwise it will only output the last iteration (the previous ones are overwritten each time)
  • The %end should come before the Run statement

You can also use the CATS function to concatenate the data and remove unwanted blanks, which will save using COMPRESS separately.  There's some slight confusion as to what you want to show in your output, should it be id1, id2 etc or d1, d2 etc ?  To achieve the former, put quotes around id in the concatenate function, otherwise leave it as is and it will take the value of the column id.  Here is the code I think you want, I've added a length statement to prevent the default $200.  You don't really need the macro either, just use a normal loop within the datastep.

%let a=5;

data l2;

length id $6;

set l;

do i= 1 to &a;

t=cats(id,i);

output;

end;

run;

proc print data=l2;

run;

Message was edited by: Keith Timms Obviously the length statement should against column t, not id.

sas_Forum
Calcite | Level 5

Thqs

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
  • 2 replies
  • 1173 views
  • 0 likes
  • 2 in conversation