BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BernyOsuna
Obsidian | Level 7

 

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.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

3 REPLIES 3
LinusH
Tourmaline | Level 20
I don't understand. Why can't they be resolved within &COL?
Data never sleeps
ballardw
Super User

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.

 

 

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 1078 views
  • 0 likes
  • 4 in conversation