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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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