Hi,
I'm trying to set up a SAS EG project where multiple macro variables are create at the beginning of the project. These variables are A, A1, A2, A3, A4 and must obey the following rules:
I define A using a prompt and All the rules are defined so my problem is with the other variables that depende on A.
In python I would just use a list and define the variables using negative indexes. Is any way to do this in SAS other than programming a condition for each variable?
Thanks in advance
Thanks @WarrenKuhfeld I used your code and modified it a bit to fit my purpose.
Here is the final version
%let Alist =1 2 3 4 5 7 8 9 10 11 12 13;
%let A = 1;
data _null_ (drop=i);
array A(4);
do i=1 to 4;
A{i} = scan("&Alist.",ifn(&A-i le 0,&A-i-1,&A-i));
call symputx(cats('A',i),A(i));
end;
run;
%put _user_;
Please tell us more on what is the purpose of creating these macro variables, what are they used for.
To process something within a loop you need to create a macro program, the code below shows an example that creates as many macro variables as you need with the proper content.
Note, that once you have a global macro variable, it is there to stayin your SAS session, unless you delete them using %SYMDEL.
%let a = 10;
%macro createVars;
%local i;
%do i = 1 %to &a;
%global a&i;
%let a&i = &i;
%end;
%mend;
%createVars
%put _user_;
Macro is really not the best methodolgy. Macro is nothing more than a text find/replace system. Its not good to try to use it for data processing.
What purpose is there in a having X amount of text macro variables which essentially hold something as simple as the formula?
Have your prompt supply A. Then whenever you want to use it, the formula is simply &A. - x where x is the iterator in question. There seems no logical reason to keep each evaluated version.
Something like this?
%let a = 2;
data _null_;
a = &a;
do i = 1 to 13;
a = ifn(a - 1 le 0, 13, a - 1);
call symputx(cats('a', i), a);
end;
run;
%put _user_;
Thanks @WarrenKuhfeld I used your code and modified it a bit to fit my purpose.
Here is the final version
%let Alist =1 2 3 4 5 7 8 9 10 11 12 13;
%let A = 1;
data _null_ (drop=i);
array A(4);
do i=1 to 4;
A{i} = scan("&Alist.",ifn(&A-i le 0,&A-i-1,&A-i));
call symputx(cats('A',i),A(i));
end;
run;
%put _user_;
OK. Glad I could help. I am not sure why you are using arrays. You certainly don't need to ever drop a variable with DATA _NULL_. The whole point of DATA _NULL_ is you are not making a data set.
You could easily generate the macro variables using a data step.
data _null_;
do i=1 to 13 ;
result=&a-i+13*(&a<=i);
put i= result= ;
call symputx(cats('a',i),result);
end;
run;
But how are you using these 13 extra macro variables? Perhaps it would just be easier to create a macro to emit the relative number you need instead of making a bunch of macro variables.
%macro relative(x);
%eval(&a-&x+13*(&a<=&x))
%mend relative ;
Then instead of referencing &A4 you could reference %RELATIVE(4).
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 25. 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.