Desktop productivity for business analysts and programmers

Dynamic names of macro variables

Accepted Solution Solved
Reply
Contributor
Posts: 73
Accepted Solution

Dynamic names of macro variables

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


Accepted Solutions
Solution
‎02-07-2014 03:43 PM
Respected Advisor
Posts: 4,969

Re: Dynamic names of macro variables

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


All Replies
Respected Advisor
Posts: 3,775

Re: Dynamic names of macro variables

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.

Solution
‎02-07-2014 03:43 PM
Respected Advisor
Posts: 4,969

Re: Dynamic names of macro variables

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.

Contributor
Posts: 73

Re: Dynamic names of macro variables

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.

Respected Advisor
Posts: 3,775

Re: Dynamic names of macro variables

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.

Contributor
Posts: 73

Re: Dynamic names of macro variables

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?

Super User
Super User
Posts: 6,323

Re: Dynamic names of macro variables

CATS() is the function you are looking for.

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

Respected Advisor
Posts: 4,969

Re: Dynamic names of macro variables

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.

Contributor
Posts: 73

Re: Dynamic names of macro variables


That was it.. thanks astounding.

It works as intended now!

Super User
Super User
Posts: 6,323

Re: Dynamic names of macro variables

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?

Grand Advisor
Posts: 10,210

Re: Dynamic names of macro variables

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.

Valued Guide
Posts: 2,174

Re: Dynamic names of macro variables

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

Grand Advisor
Posts: 10,210

Re: Dynamic names of macro variables

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


☑ This topic is SOLVED.

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

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