Anonymous
Anonymous

Reputation: 1

What will be the output of this %macro function in SAS and how to display the output in sas studio

%macro segm1;
  data _null_;
    %do i=0 %to 8;
      call symput("yyyymm_i",put(intnx('month',today(),-1,'b'),yymmn6.));
    %end;
%mend;
%segm1;
run;

What will be the output and how to get/dispaly/view output of this macro code in sas studio?

Upvotes: 0

Views: 420

Answers (2)

Dirk Horsten
Dirk Horsten

Reputation: 3845

You can print individual macro variables to the log with the %put statement, like %put &yyyymm_i;

You can print all macro variable with %put _all_;, or if you are only interested in variables you created yourself: %put _user_; (or within a macro %put _local_;)

By the way, your code is wrong, try this

%macro segm1;
  data _null_;
    %do i=0 %to 8;
      call symput("yyyymm_&i", put(intnx('month',today(),-1,'b'),yymmn6.));
    %end;
%mend;
%segm1;
run;

%put _user_;

Upvotes: 0

Tom
Tom

Reputation: 51621

Since a macro is used to generate SAS code to view the output of macro set the MPRINT option before running it. You will see that the macro generates a data statement and 8 call symput statements.

There are a lot of problems with that code.

  1. Uses %DO loop where it should be using a DO loop.
  2. Creates the same macro variable over and over.
  3. Starts a data step, but does not end it. Was this on purpose? Why?
  4. Creates LOCAL macro variables that will disappear when the macro finishes.
  5. Uses older less functional call symput() function instead of call symputx().

If you want to create 8 macro variables just use a normal DO loop. No need for a macro. Use the value of the loop variable to change the name of the macro variable generated and the month the result represents.

data _null_;
  do i=0 to 8;
    call symputx(cats('yyyymm_',i),put(intnx('month',today(),-i,'b'),yymmn6.));
  end;
run;

Which will create a series of macro variables named YYYYMM_0 to YYYYMM_8 with six digit strings like 202204 , 202203 , ... representing the current month back to eight months ago

If you did want to run that inside a macro and have the macro variables it creates available after the macro ends then set the optional third parameter to call symputx() to the string 'G' so that they are defined in the global symbol table instead of the local symbol table.

Upvotes: 2

Related Questions