BookmarkSubscribeRSS Feed
ShawnMcB
Calcite | Level 5

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

8 REPLIES 8
PhilC
Rhodochrosite | Level 12

I don't think 'i' should be in quotes in the symputx function?

Reeza
Super User

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

ShawnMcB
Calcite | Level 5

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

Reeza
Super User

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;

ShawnMcB
Calcite | Level 5

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

ballardw
Super User

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.

ShawnMcB
Calcite | Level 5

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

ballardw
Super User

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

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
  • 1210 views
  • 6 likes
  • 4 in conversation