DATA Step, Macro, Functions and more

Date change

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

Date change

So I want to build a macro that has two dates.  The first date is the starting day.  The second date is the ending day. If the ending day is on a Saturday and it is the first day of the month, then I want date2 to increase 1 day to be '02OCT2016'd in the example below.  Otherwise the date2 would be what is listed in the mend statement. 

 

 

There is code for pulling data in between, but that is not the problem.  Using SAS enterprise guide 6.1 and sas 9.4.

 

%MACRO flash (cy,date1,date2);

data _null_;

if weekday("&date2"d) = 7 and day("&date2"d) eq 1 then date2 = intnx('day',date2,1);

RUN;

proc sql;

create table test1 as

select a.*, b.*

from table 1, table2

where rdate ge "&date1"d and rdate le "&date2"d;

QUIT;

 

%MEND;

%FLASH (1,'02SEP2016'd,'01OCT2016'd);

%FLASH (2,'03SEP2016'd,'02OCT2016'd);


Accepted Solutions
Solution
‎11-17-2016 06:42 PM
Super User
Posts: 10,516

Re: Date change

I would recommend than instead of

%FLASH (1,'02SEP2016'd,'01OCT2016'd);

That you use

 

%FLASH (1,02SEP2016,01OCT2016);

 

because you are referencing things in the form of "&date2"d in the code.

 

And if this is debug code:

if weekday("&date2"d) = 7 and day("&date2"d) eq 1 then date2 = intnx('day',date2,1);

You may want

if weekday("&date2"d) = 7 and day("&date2"d) eq 1 then date2 = intnx('day',"&date2"d,1);

Put date2 date9.; /*< to display the data step variable value to you*/

Assuming that you actually will want to add a CALL SYMPUTX('DATE2',put(date2,date9.)) after you get the logic working correctly

View solution in original post


All Replies
Solution
‎11-17-2016 06:42 PM
Super User
Posts: 10,516

Re: Date change

I would recommend than instead of

%FLASH (1,'02SEP2016'd,'01OCT2016'd);

That you use

 

%FLASH (1,02SEP2016,01OCT2016);

 

because you are referencing things in the form of "&date2"d in the code.

 

And if this is debug code:

if weekday("&date2"d) = 7 and day("&date2"d) eq 1 then date2 = intnx('day',date2,1);

You may want

if weekday("&date2"d) = 7 and day("&date2"d) eq 1 then date2 = intnx('day',"&date2"d,1);

Put date2 date9.; /*< to display the data step variable value to you*/

Assuming that you actually will want to add a CALL SYMPUTX('DATE2',put(date2,date9.)) after you get the logic working correctly

Contributor
Posts: 44

Re: Date change

You own it..I just designed a sample one to test it for reference.

 


data table1 ;
input ID DOB ddmmyy10.;
datalines ;
1 02/10/2016
5 03/10/2016
;
run ;

data table2 ;
input ID DOB ddmmyy10.;
datalines ;
1 02/10/2016
5 03/10/2016
;
run ;


%MACRO flash (cy,date1,date2);

data _null_;
if weekday("&date2"d) = 7 and day("&date2"d) eq 1 then date2 = intnx('day',date2,1);
RUN;

%put &date2 ;

proc sql;
create table test1 as
select a.id,a.DOB format=date9.
from table1 as a , table2 as b
where a.DOB ge "&date1."d and a.DOB le "&date2."d;
QUIT;

%MEND;
%FLASH (1,02SEP2016,01OCT2016);
%FLASH (1,03SEP2016,02OCT2016);

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 141 views
  • 0 likes
  • 3 in conversation