SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

calling different dates into one macro variable dynamically one by one

Reply
Occasional Contributor
Posts: 15

calling different dates into one macro variable dynamically one by one

data ds;
format dates date9.;
infile datalines;
input dates date9.;
datalines;
15apr2018
18apr2018
20apr2018
21apr2018
22apr2018
;
run;

data _null_;
set ds;
call symput("dates"||compress(_N_),dates);
run;
%put   &dates1;
%macro tr;
%global x;
%do i=1 %to 5;
x=dates.&i;

%end;

%mend;
Occasional Contributor
Posts: 15

Re: calling different dates into one macro variable dynamically one by one

this code not getting.Help  me this  regard

Valued Guide
Posts: 559

Re: calling different dates into one macro variable dynamically one by one

Check this to add all values into a Macro

proc sql;
select PUT(dates,date9.) INTO : Dates Separated by " "
from ds;
quit;
%put &Dates;
Thanks,
Suryakiran
Occasional Contributor
Posts: 15

Re: calling different dates into one macro variable dynamically one by one

Posted in reply to SuryaKiran
That gives all dates into one macro variable

I need to use this variable into another macro
Every date wise it has to run one by one
Super User
Posts: 23,267

Re: calling different dates into one macro variable dynamically one by one

Look at CALL EXECUTE instead then.

 


@rajusas wrote:
That gives all dates into one macro variable

I need to use this variable into another macro
Every date wise it has to run one by one

 

Valued Guide
Posts: 559

Re: calling different dates into one macro variable dynamically one by one

Check this then,

proc sql;
select PUT(dates,date9.),count(*) INTO : Dates Separated by " ",:Count
from ds;
quit;
%put &Dates &Count;

data want;
do i=1 to &Count.;
Date=SCAN("&Dates.",i," ");
output;
end;
run;
Thanks,
Suryakiran
Super User
Posts: 9,888

Re: calling different dates into one macro variable dynamically one by one

[ Edited ]

A statement like

a=20apr2018;

is not valid in open code, and not valid (illegal name on the right side) in a data step anyway.

 

(This is one of the 5 statements created by the macro)

 

Correction: since you use a numeric format in the input, you get 5 statements

a=20000;

(numbers vary).

As mentioned, such statements are not valid in open code. They would be valid in a data step.

 

Second correction after running that code on a SAS system:

Since you do not adress your macro variables correctly, this is the result of calling the macro:

54         %tr
NOTE: Line generated by the invoked macro "TR".
54           x=dates.&i;
             _
             180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the invoked macro "TR".
54          x=dates.&i;
            _
            180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the invoked macro "TR".
54          x=dates.&i;
            _
            180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the invoked macro "TR".
54          x=dates.&i;
            _
            180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the invoked macro "TR".
54          x=dates.&i;
            _
            180

ERROR 180-322: Statement is not valid or it is used out of proper order.

 


@rajusas wrote:
data ds;
format dates date9.;
infile datalines;
input dates date9.;
datalines;
15apr2018
18apr2018
20apr2018
21apr2018
22apr2018
;
run;

data _null_;
set ds;
call symput("dates"||compress(_N_),dates);
run;
%put   &dates1;
%macro tr;
%global x;
%do i=1 %to 5;
x=dates.&i;

%end;

%mend;

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 15

Re: calling different dates into one macro variable dynamically one by one

Posted in reply to KurtBremser
In data it is yymmdd format
Super User
Posts: 9,888

Re: calling different dates into one macro variable dynamically one by one


@rajusas wrote:
In data it is yymmdd format

No. You never assign or otherwise use that format in your code.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 23,267

Re: calling different dates into one macro variable dynamically one by one

data ds;
format dates date9.;
infile datalines;
input dates date9.;
datalines;
15apr2018
18apr2018
20apr2018
21apr2018
22apr2018
;
run;

%macro print_date(date=);

%put The Date is: %sysfunc(putn("&date."d, yymmddd10.));
%put The day after the date is: %sysfunc(intnx(day, "&date"d, 1, s), yymmddd10.);

%mend;

data _null_;
set ds;

str = catt('%print_date(date=', 
            put(dates, date9.), 
            ');');
            
call execute (str);
run;
Super User
Posts: 6,629

Re: calling different dates into one macro variable dynamically one by one

[ Edited ]

Given your original program, the simplest fix would be to change this statement:

 

x=dates.&i;

 

Turn that into:

 

%let x = &&dates&i;

 

Then immediately (don't wait until after the %DO loop, and certainly don't wait for a different macro) add the macro call that should run for each date, referring to &X as needed.

Ask a Question
Discussion stats
  • 10 replies
  • 200 views
  • 0 likes
  • 5 in conversation