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


I'm having trouble creating macro variables from an array with the call symputx function.

After creating a dataset containing the coefficients from proc reg run on the following fitness dataset, I'd like to assign each of the 6 coeffcients to macro variables p1-p6.

The code runs without error, however when I attempt to view the values of p1-p6 SAS doesn't recognize the macro variables:

%put &p1 &p2 &p3 &p4 &p5 &p6;

711 %put &p1 &p2 &p3 &p4 &p5 &p6;

WARNING: Apparent symbolic reference P1 not resolved.

WARNING: Apparent symbolic reference P2 not resolved.

WARNING: Apparent symbolic reference P3 not resolved.

WARNING: Apparent symbolic reference P4 not resolved.

WARNING: Apparent symbolic reference P5 not resolved.

WARNING: Apparent symbolic reference P6 not resolved.

&p1 &p2 &p3 &p4 &p5 &p6

Any ideas what I'm doing wrong? Thanks much!

data fitness;

      input Age Weight Oxygen RunTime RestPulse RunPulse MaxPulse @@;

   datalines;

   44 89.47 44.609 11.37 62 178 182   40 75.07 45.313 10.07 62 185 185

   44 85.84 54.297  8.65 45 156 168   42 68.15 59.571  8.17 40 166 172

   38 89.02 49.874  9.22 55 178 180   47 77.45 44.811 11.63 58 176 176

   40 75.98 45.681 11.95 70 176 180   43 81.19 49.091 10.85 64 162 170

   44 81.42 39.442 13.08 63 174 176   38 81.87 60.055  8.63 48 170 186

   44 73.03 50.541 10.13 45 168 168   45 87.66 37.388 14.03 56 186 192

   45 66.45 44.754 11.12 51 176 176   47 79.15 47.273 10.60 47 162 164

   54 83.12 51.855 10.33 50 166 170   49 81.42 49.156  8.95 44 180 185

   51 69.63 40.836 10.95 57 168 172   51 77.91 46.672 10.00 48 162 168

   48 91.63 46.774 10.25 48 162 164   49 73.37 50.388 10.08 67 168 168

   57 73.37 39.407 12.63 58 174 176   54 79.38 46.080 11.17 62 156 165

   52 76.32 45.441  9.63 48 164 166   50 70.87 54.625  8.92 48 146 155

   51 67.25 45.118 11.08 48 172 172   54 91.63 39.203 12.88 44 168 172

   51 73.71 45.790 10.47 59 186 188   57 59.08 50.545  9.93 49 148 155

   49 76.32 48.673  9.40 56 186 188   48 61.24 47.920 11.50 52 170 176

   52 82.78 47.467 10.50 53 170 172

   ;

run;

proc reg data=fitness outest=params;

      model Oxygen=Age Weight RunTime RunPulse RestPulse MaxPulse;

run;

quit;

%macro assignparm(k);

data _null_;

set params;

array x[&k] Age Weight RunTime RunPulse RestPulse MaxPulse;

%do i=1 %to &k;

      call symputx(cats("p",&i),x[&i]);

%end;

run;

%mend;

%assignparm(6)

%put &p1 &p2 &p3 &p4 &p5 &p6;

1 ACCEPTED SOLUTION

Accepted Solutions
FriedEgg
SAS Employee

Your macro serves no purpose in the given scenario.  As others have mentioned, you have a scope issue with your variables.  You can use call symputx to set scope to global if there is some other reason to having this datastep in a macro.

*in open code;

data _null_;

set params;

array x

  • Age Weight RunTime RunPulse RestPulse MaxPulse;
  • do _n_ = 1 to dim(x);

    call symputx( cats( 'p' , _n_ ) , x[_n_] );

    end;

    run;

    *utilizing macro for do loop;

    %macro assignparm(k);

    data _null_;

    set params;

    array x

  • Age Weight RunTime RunPulse RestPulse MaxPulse;
  • %do i = 1 %to &k;

    call symputx( "p&k" , x[&i] , 'g' ); *set macro variable in global scope;

    %end;

    run;

    %mend;

    %assignparm(6)

    View solution in original post

    5 REPLIES 5
    Reeza
    Super User

    You have a scope issue. The macro variables are created as local variables and are gone once the macro is run.

    Change them to global macro variables instead.

    Ron_MacroMaven
    Lapis Lazuli | Level 10

    as Reeza pointed out, this is a scope issue

    %do i=1 %to &k;

        %global p&i;

    *  you can avoid the cats function call;

    *  call symputx(cats("p",&i),x[&i]);

        call symputx("p&i",x[&i]);

    %end;


    Ron Fehd  macro maven

    Astounding
    PROC Star

    Also note that CALL SYMPUTX permits you to specify a third parameter to designate the created variables as being global:

    FriedEgg
    SAS Employee

    Your macro serves no purpose in the given scenario.  As others have mentioned, you have a scope issue with your variables.  You can use call symputx to set scope to global if there is some other reason to having this datastep in a macro.

    *in open code;

    data _null_;

    set params;

    array x

  • Age Weight RunTime RunPulse RestPulse MaxPulse;
  • do _n_ = 1 to dim(x);

    call symputx( cats( 'p' , _n_ ) , x[_n_] );

    end;

    run;

    *utilizing macro for do loop;

    %macro assignparm(k);

    data _null_;

    set params;

    array x

  • Age Weight RunTime RunPulse RestPulse MaxPulse;
  • %do i = 1 %to &k;

    call symputx( "p&k" , x[&i] , 'g' ); *set macro variable in global scope;

    %end;

    run;

    %mend;

    %assignparm(6)

    RobF
    Quartz | Level 8

    Fantastic, thank you gentlemen for the very helpful comments!

    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
    • 5 replies
    • 5355 views
    • 8 likes
    • 5 in conversation