DATA Step, Macro, Functions and more

Resolve a series of macro variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Resolve a series of macro variables

Hello, I want to resolve a series of macro variables in a do loop, but the following program doesn't work. For some reason, I can't

define the array for mac1-mac3. Any idea on how to resolve them directly?

 

%let mac1=2;
%let mac2=3;
%let mac3=7;
data test;
array gg{3} gg1-gg3 ;
do i=1 to 3;
gg{i}=dequote(cats('&','mac',put(i,best.)));
end;
run;


Accepted Solutions
Solution
3 weeks ago
Super User
Posts: 10,761

Re: Resolve a series of macro variables

Posted in reply to liyongkai800
%let mac1=2;
%let mac2=3;
%let mac3=7;
data test;
array gg{3} gg1-gg3 ;
do i=1 to 3;
gg{i}=symgetn(cats('mac',i));
end;
run;

View solution in original post


All Replies
Solution
3 weeks ago
Super User
Posts: 10,761

Re: Resolve a series of macro variables

Posted in reply to liyongkai800
%let mac1=2;
%let mac2=3;
%let mac3=7;
data test;
array gg{3} gg1-gg3 ;
do i=1 to 3;
gg{i}=symgetn(cats('mac',i));
end;
run;
Occasional Contributor
Posts: 12

Re: Resolve a series of macro variables

I appreciate your help, Ksharp. This is the second time you help me in time. Thanks so much.
Super User
Super User
Posts: 9,599

Re: Resolve a series of macro variables

Posted in reply to liyongkai800

Why would you put numerical data into character macro variables, then write a datastep to put them into a series of numerical variables in a dataset using implicit conversion?  The whole process seems bizarre?

data test;
  gg1=1;
  gg2=2;
  gg3=3;
run;

This is effectively what the code does?

Occasional Contributor
Posts: 12

Re: Resolve a series of macro variables

The reason is that I calculate those macro variables in the earlier step and want to use them in later data step. My real program is like
data want;
set one;
Impose the do loop on set one(include that series of macro variables)
That might be bizarre for you, but as a fresh sas man, I can't come up with a better idea.
Super User
Posts: 10,209

Re: Resolve a series of macro variables

Posted in reply to liyongkai800

@liyongkai800 wrote:
The reason is that I calculate those macro variables in the earlier step and want to use them in later data step. My real program is like
data want;
set one;
Impose the do loop on set one(include that series of macro variables)
That might be bizarre for you, but as a fresh sas man, I can't come up with a better idea.

Anytime you have a series of data, consider to store it in a dataset. If you need it for look-up purposes, create a format from that dataset.

I basically only store singular pieces of data (like a cutoff date, or a company descriptor for filtering) in macro variables.

 

Think like that: the macro preprocessor helps you in making code dynamic. It is not designed (or meant) for handling data.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,599

Re: Resolve a series of macro variables

Posted in reply to KurtBremser

Just to add to that, you also use the ability to merge/join data as well.

Super User
Super User
Posts: 8,069

Re: Resolve a series of macro variables

Posted in reply to liyongkai800

So if you have single observation dataset with those three variables you can include into a future and there is no need to move the values into macro variables at all.

Say you have the values in a dataset name SUMMARY and you want to combine that with another dataset named HAVE to produce a dataset named WANT.  You might code something like this:

data want ;
  if _n_=1 then set summary ;
  set have ;
  * Calculations using variables from HAVE and SUMMARY ;
run;

SAS will automatically retain the values of the variables from SUMMARY onto all iterations of the data step.

Occasional Contributor
Posts: 12

Re: Resolve a series of macro variables

Thanks, I will take that for later program.
☑ This topic is solved.

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

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