Search code examples
sasmacrossas-macromacro-variable

SAS Create Macro variables using a macro


I have the following dataset:

A1  A2  A3
B   1   0.89606
B   2   0.10058
B   3   0.00336
C   1   0.88083
C   2   0.07205
C   3   0.04712
L   1   0.93198
L   2   0.06802
L   3   0.0000
R   1   0.8096
R   2   0.18359
R   3   0.00681

I want to create a macro variable for each row, this is the code I'm trying to run:

%MACRO TEST (Input);
%DO I = 1 %TO 3;
%global mac_var_&Input._&I.;
proc sql noprint;
    select  A3
    into    :mac_var_&Input._&I.
    from    data
    where   A1= "&Input." AND A2=  &I. ;
quit;

%END;
%MEND;

%TEST(R);
%TEST(B);
%TEST(L);
%TEST(C);

an example of what I'd want is:

the macro variable &mac_var_R_1 = 0.8096
the macro variable &mac_var_C_2 = 0.07205
the macro variable &mac_var_L_3 = 0.0000

Solution

  • Your posted code works for the data you posted. So if you are having trouble you need show more details. Either the actual data is not the same as your example or you are using the macro variables incorrectly.

    You can generate macro variables from every observation easily using the CALL SYMPUTX() method in a data step. You can even force them into GLOBAL macro space if the datastep is running inside a macro.

    data _null_;
      set work.data ;
      call symputx(catx('_','mac_var',a1,a2),a3),'g');
    run;