BookmarkSubscribeRSS Feed
v
Calcite | Level 5 v
Calcite | Level 5

Hello everyone,

I was wondering if there is a way to write dataset variable into a macro variable and use the macro variable within the same datastep.
I have tried different approaches to do the following; but have been running into call symput, excute so far. Thanks for any help that might help to solve the situation...

data atmp;
   input anID surfix $;
   datalines;
1 3_1
2 2_2
3 2_3
;
run;

data atmpdt;
input anID A_2_1 A_2_2 A_2_3 B_2_1 B_2_2 B_2_3;
datalines;
1 0.1 0.2 0.3 0.4 0.5 0.6
2 1.1 1.2 1.3 1.4 1.5 1.6
3 2.1 2.2 2.3 2.4 2.5 2.6
;
run;

%let prefixlist=A B;

data atmp2;
Merge atmp atmpdt; by anID;
/*Intention: read current row variable surfix into the macro variable asurfix*/
call symput('Asurfix',surfix);

nbvar = countw("&prefixlist"," ");
/*Intention: for each name in the macro variable "prefixlist"
create a dataset set variable initiliazed with the value in the var &aprefix._&Asurfix  */
   do count = 1 to nbvar;
    call symput('aprefix',scan("&prefixlist",count," "));
  &aprefix=&aprefix._&Asurfix;
   end;
run;

3 REPLIES 3
data_null__
Jade | Level 19

Why?

Look at SYMGET and RESOLVE functions.

PaigeMiller
Diamond | Level 26

To tell you the truth, I wouldn't do this with macros at all, I would use PROC TRANSPOSE.

proc transpose data=atmpdt out=a;
by anID;
run;

data aa;
merge a atmp;
by anID;
run;

Now you can assign A and B to the proper variables by matching text strings in data set AA, and throw out the observations in aa that don't match;

--
Paige Miller
Patrick
Opal | Level 21

SAS variables get created during the compilation phase of a data step. It is not possible to create additional SAS variables during execution phase.

SAS(R) 9.3 Language Reference: Concepts, Second Edition

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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