DATA Step, Macro, Functions and more

macro loop error

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

macro loop error

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;


Accepted Solutions
Solution
‎10-09-2011 01:53 PM
PROC Star
Posts: 7,363

macro loop error

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


All Replies
Super User
Super User
Posts: 6,500

macro loop error

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.

Occasional Contributor
Posts: 14

macro loop error

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

PROC Star
Posts: 7,363

macro loop error

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.

Occasional Contributor
Posts: 14

macro loop error

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

PROC Star
Posts: 7,363

macro loop error

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;

Occasional Contributor
Posts: 14

macro loop error

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)]

PROC Star
Posts: 7,363

macro loop error

What do you want the macro variables to be called?

Occasional Contributor
Posts: 14

macro loop error

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 .

Solution
‎10-09-2011 01:53 PM
PROC Star
Posts: 7,363

macro loop error

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;

Occasional Contributor
Posts: 14

macro loop error

Great! Thanks art.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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