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

Hello EG Users,

Is it possible to code dynamic names of macro variables?

In code snippet below, I would like to have as many macro variables as number of observations in MissedDaysInput

data work.MissedDays (drop=i MDayB);

set work.MissedDaysInput nobs=MissedDaysInputLength;

do i = 1 to MissedDaysInputLength;

%Let <DynamicVariableName>= MDayB;

MissedDaysLength = MissedDaysInputLength;

end;

format MDay DATE9.;

run;

quit;

I was considering using concat function:

concat(MissedDay,i);

but not sure how to make that string name of the macro variable. It is easily possible to save value of macro variable as that dynamic concatenated string but how do I assign name of macro variable as that concatenated string.

Thanks,

Dhanashree

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Agreed.  Look at what your code would do if it actually worked.  On the first observation from your data set, you would create a boatload of macro variables that all have the same value.  Then on the second observation from your data set, you would replace that same boatload with a new value.  You would be left with a boatload of macro variables, all having the value of MDayB from the last observation in your data set.  It's not very likely that this result would be satisfying.  Perhaps you would like to share a little more about what you are trying to accomplish.

View solution in original post

12 REPLIES 12
data_null__
Jade | Level 19

You can create macro variables and name them using data values with CALL SYMPUTX.  However I not sure you need to do that.  Can you explain what your are trying to do.  Use the "have/need scenario"  where you show the data you have and the data or other result that want/need.

Astounding
PROC Star

Agreed.  Look at what your code would do if it actually worked.  On the first observation from your data set, you would create a boatload of macro variables that all have the same value.  Then on the second observation from your data set, you would replace that same boatload with a new value.  You would be left with a boatload of macro variables, all having the value of MDayB from the last observation in your data set.  It's not very likely that this result would be satisfying.  Perhaps you would like to share a little more about what you are trying to accomplish.

noobs
Fluorite | Level 6

Here it is:

data work.MissedDays (drop=i MDayB);

set work.MissedDaysInput nobs=MissedDaysInputLength;

do i = 1 to MissedDaysInputLength;

%Let value = MDayB;

MDay = &value;

MissedDaysLength = MissedDaysInputLength;

end;

format MDay DATE9.;

run;

quit;

Assuming dataset MissedDaysInput contains two observations:

01FEB2014

02FEB2014

Output dataset MissedDays will contain

01FEB2014      2

02FEB2014      2

Which is fine.

In this case, macro variable value is overwritten and its final value is 02FEB2014.

However I want to create distinct macro variables with names missedday1 that contains 01FEB2014 and missedday2 that contains 02FEB2014; so that I can use them in another data step and set one of the variables in output dataset equal to value in missedday1 (01FEB2014); process it and output to final dataset; then change it to value in missedday2 (02FEB2014); process it and append to final dataset. This is expected to continue for all missed days which can be variable every single time I run this program.

In summary, I am looking for solution to replace value variable above with multiple macro variables.

Something like

%Let concat(missedday, i) = MDayB

But this will again give me just one macro variable and not multiple macro variables

misseday1, misseday2

I hope I made it clear.

data_null__
Jade | Level 19

Well it is not clear to me, others I don't know.  I asked you to show what you have and what you want it to become, you can leave out all the parts about the macro variables and the way you are thinking of it now.  That is just confusing me and I think you too.

Just show example of the data(s) you have and what you want the result to look like.  No need to include any code or psudo code.  Althought including code to read the sample data will make it easier.  For those what will help you.

noobs
Fluorite | Level 6

I think I almost have nailed it... except for one error that I am getting for CONCAT function

call symput(concat('MissedDay',i),MDayB);

                        ______

                        68

ERROR 68-185: The function CONCAT is unknown, or cannot be accessed.

Any idea what will resolve it.. is there something special that needs to be considered before using concat function?

Tom
Super User Tom
Super User

CATS() is the function you are looking for.

But you still haven't answered WHY you would want to create so many macro variables.

Astounding
PROC Star

It sounds like you're getting close, and that the programming is actually much simpler than what you started with.  The entire DO loop disappears, and gets replaced with a single statement:

call symputx( cats('missed_day', _n_) , put(MDayB, date9.) );

Remember, the DATA step automatically loops.  You don't need to add a DO loop to make that happen.  The DATA step loops through each incoming observation, performing the programming statements for each.

Good luck.

noobs
Fluorite | Level 6


That was it.. thanks astounding.

It works as intended now!

Tom
Super User Tom
Super User

Why would you move the value to a macro variable when you already have it in a data set?  Why not just combine the datasets?

ballardw
Super User

I have used a similar approach to create the names of output files so my customers could find the right report from the file name.

Peter_C
Rhodochrosite | Level 12

Often create a collection of parameters in this way.

libname parms "&parameter_workbook" access= readonly ;

* the workbook has a range named PARAMETERS with two columns. The first is headed NAME  and the second VALUE ;

Data _null_ ;

Set parms.PARAMETERS ;

where name ne ' ' ;

call symputx( name, value ) ;

run ;

* only the range name is case sensitive. ;

ballardw
Super User

With the OP wanting variables with the count appended it may also be a good idea to create a macro variable to say how many of those variables you created so you can use it as a loop counter.

at the end of your data step creating the variables add:

call symput ('NumVars',put(_n_,f5.));


SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 4786 views
  • 0 likes
  • 6 in conversation