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;
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;
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.
how can i define then for negative values? any suggestion!
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.
i want to save the past month start date values into macro variables.how can this be done in above case?
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;
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)]
What do you want the macro variables to be called?
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 .
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;
Great! Thanks art.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.