I have couple of macro variables which has values and I want to create a list of those values for each variable into one and the final list should be
%let xp1 = 2;
%let xp2 = 3;
%let xp3 = 1;
%do i=1 %to 3;
%if &i=1 %then %do;
%let y = &&xp&i._crs;
%end;
%else %do;
%let y =%sysfunc(catx(|,&y,&&xp&i._crs));
%end;
%end;
%put &y;
so value of y should be 2|3|1 after the loop is completed
Can anyone please help
And i am baffled why this is troubling me
%let xp1 =2;
%let xp2 =3;
%let xp3 =1;
/*works fine*/
%let list=%sysfunc(catx(|,&xp1,&xp2,&xp3));
%put &list;
/*does not work*/
%macro l;
%let list=%str();
%do i=1 %to 3;
%put &&xp&i;
%let list=%sysfunc(catx(|,&list,&&xp&i));
%end;
%mend l;
%l
%put &list;
oh the % let statements should be like below
%let xp1_crs = 2;
%let xp2_crs = 3;
%let xp3_crs = 1;
%let xp1 =2;
%let xp2 =3;
%let xp3 =1;
%macro l;
%let list=%str();
%do i=1 %to 3;
%put &&xp&i;
%let list=&list|&&xp&i;
%end;
%mend l;
%l
And i am baffled why this is troubling me
%let xp1 =2;
%let xp2 =3;
%let xp3 =1;
/*works fine*/
%let list=%sysfunc(catx(|,&xp1,&xp2,&xp3));
%put &list;
/*does not work*/
%macro l;
%let list=%str();
%do i=1 %to 3;
%put &&xp&i;
%let list=%sysfunc(catx(|,&list,&&xp&i));
%end;
%mend l;
%l
%put &list;
Yes . the first code would work and can be used when we have three variables . But I wanted to do this in a do loop since I don't want to mention &x1 , &x2 , &x3 as I have 250 variables from x1 to x250
Thank you
did you try the previous post of mine
here again
%let xp1 =2;
%let xp2 =3;
%let xp3 =1;
%macro l;
%let list=%str();
%do i=1 %to 3;
%put &&xp&i;
%let list=&list|&&xp&i;
%end;
%mend l;
%l
that works for your many variables as the below test confirms
%macro c;
%do i=1 %to 250;
%let xp&i=&i;
%end;
%let list=%str();
%do i=1 %to 250;
%put &&xp&i;
%let list=&list|&&xp&i;
%end;
%mend c;
%c
%put &list;
hi
I have tried this but I am getting like below it started with a pipe
|2|3|1
thought it should be like this
2|3|1
Thank you
i assumed you would have done this simple adjustment
%put %substr(&list,2);
or you could even try extracting from macro dictionary
%let xp1 =2;
%let xp2 =3;
%let xp3 =1;
/*filter macro dictionary if name=:'XP'*/
data _null_;
do until(l);
set sashelp.vmacro(keep=name) end=l;
length temp $20;
if name=:'XP' then temp=catx('|',temp,cats('&',name));
if l then call symputx('list',temp);
end;
run;
%put list=&list;
LOG:
978
979 %put list=&list;
list=2|3|1
A simple change gets rid of the extra pipe:
%macro c;
%global list;
%local i;
%let list = &xp1;
%do i=2 %to 250;
%let list=&list|&&xp&i;
%end;
%mend c;
%c
%put &list;
wouldn't that miss xp1 value?
Edit: Sorry let me run and understand. My apologies
I hate this editor.
No, the XP1 value is captured by the first %LET statement.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.