BookmarkSubscribeRSS Feed
sasstats
Fluorite | Level 6

Hello,

I have defined a global macro variable quantile and want to use it in a sas macro within a double loop. The macro and loops look like this:

%global year quantile;

 

%macro test;

%do year=2017 %to 2019;

 

  %macro choose;

    %do qual=1 %to 3;
      %let quantile=%scan(&Matchquality.,&qual.);

       /* some further steps in this loop */

      %end;

    %end;

  %mend choose;

 

  /* invocation of other macros which are using the macro variable quantile */

 

/* Before doing the loop for new year, I would like to delete the value of quantile without loosing its status 'global' */

%mend test;

 

My question is, if it is possible to delete the contents/value of the macro variable quantile within the macro test without deleting the whole macro variable?

My problem is that the macro variable quantile has to be global. By deleting it, it loses the status global because it is defined in the sas macro choose.

 

Thank you for your answer

sasstats

 

 

 

 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

My question is, if it is possible to delete the contents/value of the macro variable quantile within the macro test 

 

Place this where it belongs

 

%let quantile=;

 

I suggest a re-write of the logic and sequencing of your macros. Nested macro definitions is not a good idea. With your nested macro definitions, you have an %end; in the wrong place. It's also not clear to me why you need the %global statement, but it doesn't hurt anything here.

 

%global year quantile;

%macro choose;
    %do qual=1 %to 3;
        %let quantile=%scan(&Matchquality.,&qual.);
              /* some further steps in this loop */
    %end;
%mend choose;

%macro test;
    %do year=2017 %to 2019;
        %choose
    %end;
%mend test;

%test

 

--
Paige Miller
Tom
Super User Tom
Super User

If you want to change the value of a macro variable just use a %LET statement.

%let quantile=;

What are you actually trying to do?

 

First thing is do NOT define macros inside of each other.  That will just confuse you.  Unlike macro variables there is no "scope" for a macro definition. All of the macros are defined in the same name space.

%macro choose;
%do qual=1 %to 3;
    %let quantile=%scan(&Matchquality.,&qual.);
       /* some further steps in this loop */
%end;
%mend choose;

%macro test;
%do year=2017 %to 2019;
  /* invocation of other macros which are using the macro variable quantile */
  /* Before doing the loop for new year, 
     I would like to delete the value of quantile without loosing its status 'global' 
  */
  %let quantile=;
%end ;
%mend test;

Other comments. 

 

Use LOCAL macro variables.  For example QUAL looks like a local macro variable for the %CHOOSE() macro and YEAR looks like a local macro variable for the %TEST() macro.

 

Use parameters to pass values into the macros.  Do not just reference "magic" macro variables in the middle of a macro definition.  Like the way that the %CHOOSE() macro is just assuming that the macro variable Matchquality will have magically been defined with a value before the macro started running.

Astounding
PROC Star

While the replies you have seen so far are both correct, I'm going to take a different path.  After all, the code you have posted would have no need to remove the macro variable &quantile, since it would just get replaced whenever %choose executes.  However ...

 

The code you have posted doesn't actually assign a value to &quantile.  It never actually runs %choose.  Perhaps that is the reason you have having some difficulty here.  

 

Perhaps  you like the structure to your program and don't want to follow the suggestions about not defining a macro within another macro.  That is still very possible here.  There is no need to define %choose.  You could just remove the %macro choose and %mend choose statements, and let the logic execute without defining a macro for it.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 351 views
  • 0 likes
  • 4 in conversation