how to create and use macro variable within data step or any alternative?

Reply
v
N/A
Posts: 1

how to create and use macro variable within data step or any alternative?

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;

Respected Advisor
Posts: 3,777

Re: how to create and use macro variable within data step or any alternative?

Why?

Look at SYMGET and RESOLVE functions.

Trusted Advisor
Posts: 1,631

Re: how to create and use macro variable within data step or any alternative?

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;

Respected Advisor
Posts: 3,908

Re: how to create and use macro variable within data step or any alternative?

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

Ask a Question
Discussion stats
  • 3 replies
  • 207 views
  • 0 likes
  • 4 in conversation