Here is my question. I want to create several variables to store the values, where the suffix is an index, and strings are changed based on different conditions. However, the following code doesn't generate the expected output.
%let var1=left;
%let var2=center;
%let var3=right;
%macro example;
%do i=1 %to 3;
%do j=1 %to 2;
%global list_&&var&i_&j;
data whatever;
list_&&var&i_&j=1
run;
%end;
%end;
%mend;
%example;
I want the macros resolved separately to have list_left_1, list_left_2,list_center_1,list_center_2,list_right_1,list_right_2. How can I get them? Thanks.
You need a dot to separate the macro variable name from the following text. In this case because you have a double ampersand, you need a double dot.
%global list_&&var&i.._&j;
Same thing here, and you left out a semi-colon as well
data whatever;
list_&&var&i.._&j=1;
run;
You need a dot to separate the macro variable name from the following text. In this case because you have a double ampersand, you need a double dot.
%global list_&&var&i.._&j;
Same thing here, and you left out a semi-colon as well
data whatever;
list_&&var&i.._&j=1;
run;
Hi @liyongkai800 Adding on to what @PaigeMiller clearly explained, I am wondering do you really want many datasteps or the code should be
/*see the commented part*/
%let var1=left;
%let var2=center;
%let var3=right;
%put &var1;
/*options symbolgen;*/
%macro example;
data whatever;
%do i=1 %to 3;
%do j=1 %to 2;
%global list_&&var&i.._&j;
/*data whatever;*/
list_&&var&i.._&j=1;
/*run;*/
%end;
%end;
run;
%mend;
%example;
If you are looking to add multiple variables to the data set as a single row:
%macro example; data whatever; %do i=1 %to 3; %do j=1 %to 2; list_&&var&i.._&j=1 ; %end; %end; run; %mend;
It really helps to show what the desired data set should actually look like.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: