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

i have this below code working fine for positive parameters but throws an error for negative values.

could anyone help!

%macro doit(m,n);

%do i=&m %to &n;

%global st&i ;

data _null_;

  st=intnx('month',today(),&i,'b');

  call symput("st&i",put(st,date9.));

%end;

%mend;

%doit(-2,-1)

%put &st-1 &st-2;

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Would something like the following suffice?

%macro doit(m,n);

  %do i=&m %to &n;

    data _null_;

      st=intnx('month',today(),&i,'b');

      %if &i lt 0 %then %do;

        %global before%eval(0-&i);

        call symput("before"||strip(%eval(0-&i)),put(st,date9.));

      %end;

      %else %do;

        %global after&i;

        call symput("after"||strip(&i),put(st,date9.));

      %end;

    run;

  %end;

%mend;

%doit(-2,-1)

%put &before1 &before2;

%doit(1,2)

%put &after1 &after2;

View solution in original post

10 REPLIES 10
Tom
Super User Tom
Super User

It helps to post the error messages:

ERROR: Invalid symbolic variable name -.

ERROR: Invalid symbolic variable name 2.

You cannot define a macro variable named ST-1 or ST-2.

Kethy
Calcite | Level 5

how can i define then for negative values? any suggestion!

art297
Opal | Level 21

How do you want to use it?  You count always just assign a counter.  The values can be negative but, like Tom said, the negatives can't be included in the macro variable name.

Kethy
Calcite | Level 5

i want to save the past month start date values into macro variables.how can this be done in above case?

art297
Opal | Level 21

Not sure if I fully understand, but do you want something like:

%macro doit(m,n);

  %do i=&m %to &n;

    %global last&i;

    data _null_;

      st=intnx('month',today(),%eval(0-&i),'b');

      call symput("last"||strip(&i),put(st,date9.));

    run;

  %end;

%mend;

%doit(1,2)

%put &last1 &last2;

Kethy
Calcite | Level 5

Right now im creating the macro variable "st" based on the i values as st&i,

when %doit(1,2) is called st1 and st2 will be the macro variables being created(no issues here)

when %doit(-2,-1) is called as pointed out by you and Tom negatives cannot be included in macro variables,  so in that case is there any better way where i can create a macro variable(instead of st&i) which would work fine in both the cases [ %doit(1,2) and %doit(-2,-1)]

art297
Opal | Level 21

What do you want the macro variables to be called?

Kethy
Calcite | Level 5

I mean when the macro %doit() is called irrespective of the positive or negative values being passed as parameters, it should create a macro variable which will hold the start of the month date corresponding to its values passed .

art297
Opal | Level 21

Would something like the following suffice?

%macro doit(m,n);

  %do i=&m %to &n;

    data _null_;

      st=intnx('month',today(),&i,'b');

      %if &i lt 0 %then %do;

        %global before%eval(0-&i);

        call symput("before"||strip(%eval(0-&i)),put(st,date9.));

      %end;

      %else %do;

        %global after&i;

        call symput("after"||strip(&i),put(st,date9.));

      %end;

    run;

  %end;

%mend;

%doit(-2,-1)

%put &before1 &before2;

%doit(1,2)

%put &after1 &after2;

Kethy
Calcite | Level 5

Great! Thanks art.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 5266 views
  • 0 likes
  • 3 in conversation