Hi all -
Extending methods seen in a SUGI online paper I am trying to create a series of macro variables on the fly using a DO LOOP and SYMPUTX in a DATA _NULL_ step.
My code and log output from a %put statement are below.
Seemingly, this program only creates the first of an intended three macro variables, while assigning it the third value in the reference array.
Any thoughts as to why, and correction much appreciated.
Thanks, Shawn
/***********************/
data _null_;
set &tbdta;
array ciset {*} ci1 ci2 ci3;
do i=1 to dim(ciset);
call symputx('var' || 'i',vname(ciset{i}));
end; stop; run;
%put _user_;
GLOBAL DIM_VAR 3
GLOBAL MSTRDRV \\Confidential\CoreT
GLOBAL SYSYR 2015
GLOBAL TBDTA tbdtawrk.tbcsdta_2009_06aug2015_int
GLOBAL VARI ci3
GLOBAL YRCTO 1916
I don't think 'i' should be in quotes in the symputx function?
You've placed the character i, not the variable i in the call symput code. I'll usually explicitly convert it and left align as well.
Try:
call symputx('var' || put(i, 2. -l) ,vname(ciset{i}));
Thanks Reeza and PhilC -
Explicit conversion using PUT helped, but still getting mismatch where only one macro is created, and assigned value of third element in array.
Will keep trying.
Thanks, Shawn
You don't need a STOP, but otherwise works fine for me.
data _null_;
array ciset {*} ci1 ci2 ci3;
do i=1 to dim(ciset);
call symputx('var' || put(i, 2. -l),vname(ciset{i}));
end; run;
%put &var1;
%put &var2;
%put &var3;
Thanks Reeza -
Explicitly transforming i with PUT and L justify worked like a charm. Along with omitting SET and STOP.
First time for everything.
Thanks again.
Shawn
Code as modified by Reeza is working for me.
I am curious as to where this is going. I'm sure there is a reason for creating the macro variables but I'm not quite sure why, with the example given, you're not using 3 %let statements. The SET statement does nothing for the example code.
Thanks Ballardw -
Thanks for correction of omitting SET. Got Reeza's code to work great. Where I am using this is to allow dynamic on the fly assignment of macros to select variables to run in accompanying programming.
Thanks to both.
Shawn
Understand need for the variables but the approach shown isn't very dynamic.
See if this makes sense in your application:
%macro MkVars(list=, stem=);
/* creates macro variables with values as the words from space delimited list
named beginning with the stem value and incremented
up to user at this point to ensure List is not blank, stem is a single "word" and
starts a valid sas macro variable name*/
%do i=1 %to %sysfunc(countw(&list,' ')) ;
%global &stem&i;
%let &stem&i = %scan(&list,&i);
%end;
%mend;
%MkVars(list= a b c, stem=letter);
%put _user_;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.