SMW
SMW

Reputation: 502

Do Loop using CALL SYMPUT

Having some problems with Do Loop Concepts. I have a static date (can be any date for that matter) defined with -

%LET DATE = %SYSFUNC(TODAY());
%PUT &DATE;

I need to create a series of macro variables that hold values of that date (&DATE) incremented by 10 days, so I used a simple data step to achieve this -

DATA _NULL_;
CALL SYMPUT('DATE10',&DATE+10);
CALL SYMPUT('DATE20',&DATE+20);
CALL SYMPUT('DATE30',&DATE+30);
RUN;

This method is OK for increments of 10 up to 30 days after the initial value of &DATE. I am now tasked with extending the report to execute on dates that extend to 250 days (incremented by 10 days) from the value of &DATE. Assuming a DO LOOP would be the most efficient execution method, I am having trouble understanding how the loop would "create" a new macro var (ex. &Date150) within the loop. Assuming the syntax below is correct, I am not sure what the next/correct step would be :

DATA _NULL_;
DO I=10 TO 150 BY 10;
CALL SYMPUT('DATE10',&DATE);
END;
RUN;

How would I "increment" the actual name of the macro var (&DATE10,&Date20...&Date150) in the loop while executing the creation of a macro var based on 10 day increments?

Upvotes: 0

Views: 3807

Answers (3)

Tom
Tom

Reputation: 51566

Consider placing the values into a single macro variable. As long as the list is not longer than maximum length of a macro variable.

DATA _NULL_;
  length dates $32767 ;
  date=today();
  DO I=10 TO 150 BY 10;
    dates=catx(' ',dates,date+i);
  end;
  CALL SYMPUTx('dates',dates);
RUN;

Then in your reporting code you can either just use the list of dates.

proc report ;
  where date in (&dates);
 ...
run;

Or if you have macro you can use a %DO loop.

%do i=1 %to %sysfunc(countw(&dates));
  %let date=%scan(&dates,&i);
  proc report;
    where date=&date;
    ....
%end;

Upvotes: 1

Reeza
Reeza

Reputation: 21264

Use the I variable as part of your variable name, via a concatenation function, cats() is probably appropriate. Also, a personal preference, but I prefer Call SymputX as it removes any extra spaces.

DATA _NULL_;
DO I=10 TO 150 BY 10;
CALL SYMPUTX(cats('DATE', i), &DATE+i);
END;
RUN;

Upvotes: 2

user667489
user667489

Reputation: 9569

Easy enough - pass in a variable as the first argument to call symput that contains the name of the macro variable you want to create.

Upvotes: 0

Related Questions