DATA Step, Macro, Functions and more

MACRO VARIABLES IN A MACRO VARIABLE WITHOUT RESOLVING THEM YET

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

MACRO VARIABLES IN A MACRO VARIABLE WITHOUT RESOLVING THEM YET

 

How can i place 3 macro variables inside of another macro variable without resolving them until it reaches a data step:

 

%LAT ALL1 = ID_1 NM_1 ID_2 NM_2... NM_10; 

%LAT ALL2 = ID_21 NM_21 ID_22 NM_22... NM_30;

%LAT ALL3 = ID_31 NM_31 ID_32 NM_32... NM_40;

 

%LET COL = &ALL1 &ALL2 &ALL3;

 

DATA SIMON;

RETAIN &COL.;

SET SIMON;

RUN;

 

DATA SIMON;

RETAIN ID_1 NM_1 ID_2 NM_2 ... NM_40;

SET SIMON;

RUN;

 

THE NUMBER OF ALL# VARIABLES CAN CHANGE SO I CAN'T HARD CODED, IT CAN BE A TIME THAT WOULD BE UP TO &ALL5.

 

 


Accepted Solutions
Solution
‎05-09-2017 09:28 AM
Super User
Super User
Posts: 6,500

Re: MACRO VARIABLES IN A MACRO VARIABLE WITHOUT RESOLVING THEM YET

Easiest way is to use CALL SYMPUTX(). That way the value can be quoted with single quotes to prevent macro expansion, but the single quotes are not part of the value.

data _null_;
  call symputx('COL','&ALL1 &ALL2 &ALL3');
run;

View solution in original post


All Replies
Super User
Posts: 5,256

Re: MACRO VARIABLES IN A MACRO VARIABLE WITHOUT RESOLVING THEM YET

I don't understand. Why can't they be resolved within &COL?
Data never sleeps
Super User
Posts: 10,500

Re: MACRO VARIABLES IN A MACRO VARIABLE WITHOUT RESOLVING THEM YET

Are any of those named variables in your data set Simon? If so

DATA SIMON;

RETAIN &COL.;

SET SIMON;

RUN;

 

Does nothing that this doesn't:

DATA SIMON;

SET SIMON;

RUN;

Retained variables that are in a dataset on the SET statement are overwritted by the value in dataset each time the Set executes.

The frequent use of the

Data simon;

   set simon;

run;

program structure (in and out the same dataset name) is asking for a headache at some point trying to find where did my variable go or how did it get that value.

 

By Any chance are you looking for a way to only have the variables in the resulting data set?

Perhaps a KEEP statement?

 

data new;

   set simon (keep= Id_1-Id_10 Id_21-Id_40 NM_1-NM_10 NM_21-NM_40 );

run;

Note the use of variable list for variables with common name varying in an index value.

 

 

Solution
‎05-09-2017 09:28 AM
Super User
Super User
Posts: 6,500

Re: MACRO VARIABLES IN A MACRO VARIABLE WITHOUT RESOLVING THEM YET

Easiest way is to use CALL SYMPUTX(). That way the value can be quoted with single quotes to prevent macro expansion, but the single quotes are not part of the value.

data _null_;
  call symputx('COL','&ALL1 &ALL2 &ALL3');
run;
☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 184 views
  • 0 likes
  • 4 in conversation