DATA Step, Macro, Functions and more

Increment based on macro

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 140
Accepted Solution

Increment based on macro

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


Accepted Solutions
Solution
‎07-06-2012 06:06 AM
Regular Contributor
Posts: 151

Re: Increment based on macro

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


All Replies
Solution
‎07-06-2012 06:06 AM
Regular Contributor
Posts: 151

Re: Increment based on macro

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.

Frequent Contributor
Posts: 140

Re: Increment based on macro

Thqs

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 219 views
  • 0 likes
  • 2 in conversation