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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
%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

8 REPLIES 8
Ksharp
Super User
%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;
liyongkai800
Obsidian | Level 7
I appreciate your help, Ksharp. This is the second time you help me in time. Thanks so much.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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?

liyongkai800
Obsidian | Level 7
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.
Kurt_Bremser
Super User

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

Tom
Super User Tom
Super User

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.

liyongkai800
Obsidian | Level 7
Thanks, I will take that for later program.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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