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

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

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

23 REPLIES 23
Cynthia_sas
SAS Super FREQ
Hi, where does the "_crs" fit -- when you show the value of &Y, you do NOT show the string _crs.

I would expect you to get messages of the form:
WARNING: Apparent symbolic reference XP1_CRS not resolved.
and I would expect you'd see those messages for each of your XP1, XP2 and XP3 macro variables.

What is the purpose of the _crs???

Cynthia
jhh197
Pyrite | Level 9

 

oh the % let statements should be like below

 

%let xp1_crs = 2;

%let xp2_crs = 3;

%let xp3_crs = 1;

novinosrin
Tourmaline | Level 20

%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
novinosrin
Tourmaline | Level 20

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;
jhh197
Pyrite | Level 9

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

novinosrin
Tourmaline | Level 20

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
novinosrin
Tourmaline | Level 20

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;
jhh197
Pyrite | Level 9

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

novinosrin
Tourmaline | Level 20

i assumed you would have done this simple adjustment

 

%put %substr(&list,2);

novinosrin
Tourmaline | Level 20

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

jhh197
Pyrite | Level 9
Hi Novin,
Thank you so much for trying different ways for helping me

Thanks
Astounding
PROC Star

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;
novinosrin
Tourmaline | Level 20

wouldn't that miss xp1 value?

 

Edit: Sorry let me run and understand. My apologies

Astounding
PROC Star

I hate this editor.

 

No, the XP1 value is captured by the first %LET statement.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 23 replies
  • 16479 views
  • 3 likes
  • 5 in conversation