DATA Step, Macro, Functions and more

Using data _null_ and symputx

Reply
Occasional Contributor
Posts: 6

Using data _null_ and symputx

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

Regular Contributor
Posts: 156

Re: Using data _null_ and symputx

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

Super User
Posts: 17,863

Re: Using data _null_ and symputx

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

Occasional Contributor
Posts: 6

Re: Using data _null_ and symputx

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

Super User
Posts: 17,863

Re: Using data _null_ and symputx

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;

Occasional Contributor
Posts: 6

Re: Using data _null_ and symputx

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

Super User
Posts: 10,516

Re: Using data _null_ and symputx

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.

Occasional Contributor
Posts: 6

Re: Using data _null_ and symputx

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

Super User
Posts: 10,516

Re: Using data _null_ and symputx

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

Ask a Question
Discussion stats
  • 8 replies
  • 315 views
  • 6 likes
  • 4 in conversation