DATA Step, Macro, Functions and more

Create Macro variable from array

Accepted Solution Solved
Reply
Contributor
Posts: 25
Accepted Solution

Create Macro variable from array

[ Edited ]

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:

 

  • A is introduced by the user and must be an integer between 1 and 13
  • A1 = A-1; A2=A-2 ....
  • if any of the variables is below 1 than it should go from 13 backwords

 

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

 


Accepted Solutions
Solution
‎01-03-2018 10:06 AM
Contributor
Posts: 25

Re: Create Macro variable from array

[ Edited ]
Posted in reply to WarrenKuhfeld

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_;

View solution in original post


All Replies
SAS Super FREQ
Posts: 810

Re: Create Macro variable from array

Posted in reply to Ricardo_Neves

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_;
Contributor
Posts: 25

Re: Create Macro variable from array

Posted in reply to Bruno_SAS
Hi, This project will run in cycles (13 each year), and in some processes I have to check the results of previous cycles (sometimes up to 4 cycles back). For that I defined these variables so that every time I run the project it will automatically find the tables marked as the previous cycle (for example we are now on cylce 13 so I need to check some info from cycle 12, 11, 10...).
Since will be starting a new year, this will reset, so in January will start cycle 1 of 2018, but I still need to check cycles 13, 12, 11 and 10 of 2017, and when a cycle 2 starts I need cycle 1 of 2018, 1, 13, 12 ,11.

What I want is for the variables A1, A2 A3 to update automatically when I define A.
Super User
Super User
Posts: 9,441

Re: Create Macro variable from array

Posted in reply to Ricardo_Neves

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.

SAS Super FREQ
Posts: 497

Re: Create Macro variable from array

Posted in reply to Ricardo_Neves

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_;
Solution
‎01-03-2018 10:06 AM
Contributor
Posts: 25

Re: Create Macro variable from array

[ Edited ]
Posted in reply to WarrenKuhfeld

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_;
SAS Super FREQ
Posts: 497

Re: Create Macro variable from array

Posted in reply to Ricardo_Neves

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.

Contributor
Posts: 25

Re: Create Macro variable from array

Posted in reply to WarrenKuhfeld
Yes you're right ,for this particular case it is not necessary, but I will have to do other calculations inside the loop using the obtained value. That is why I'm using them
Super User
Super User
Posts: 7,938

Re: Create Macro variable from array

[ Edited ]
Posted in reply to Ricardo_Neves

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).

 

☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 247 views
  • 3 likes
  • 5 in conversation