DATA Step, Macro, Functions and more

Date Increment

Accepted Solution Solved
Reply
Frequent Contributor
Frequent Contributor
Posts: 83
Accepted Solution

Date Increment

Hello all,

 

I'm creating a date from a file name.

 

%let Today_chk = %sysfunc(inputn(%qsubstr(&fldate,5,2)%qsubstr(&fldate,7,2)%qsubstr(&fldate,3,2),mmddyy6.),date9.);
%put &Today_chk;

I'm trying to increment this date by 1 day and can't seem to get it to work.

 

I've tried intnx but it doesnt want to work for me.

%let Today_chk = intnx('day',%sysfunc(inputn(%qsubstr(&fldate,5,2)%qsubstr(&fldate,7,2)%qsubstr(&fldate,3,2),mmddyy6.),date9.),1);
%put &Today_chk;

 

What am I stumbling on? 

 

 


Accepted Solutions
Solution
‎02-19-2016 03:35 PM
Super User
Super User
Posts: 6,501

Re: Date Increment

[ Edited ]

If you have a macro variable that contains an 8 digit string in the format YYYYMMDD you can convert it to an actual date value using the INPUTN() function which you can call using the %SYSFUNC() macro function.

 

%let date=%sysfunc(inputn(&fldate,yymmdd8));

 

This will be the number of days since 1/1/1960. If you want it formatted in a particular way you can use the optional format specification on the %SYSFUNC() function call.

 

%let datestr=%sysfunc(inputn(&fldate,yymmdd8),date9);

 

 

If you are using the value in an expression in SAS code (like an IF or WHERE statement) then you can use either format, but with the second you need to express it in the form of a date literal.

 

where visdate <= &date ;

where visdate <= &date + 1;

where visdate <= "&datestr"d ;

where visdate <= "&datestr"d+1 ;

 

If you need to generate the next date using the same YYYYMMDD format then use the PUTN() function and the YYMMDDN format.

 

%let nxtdate = %sysfunc(putn(1+%sysfunc(inputn(&fldate,yymmdd8)),yymmddn8));

View solution in original post


All Replies
Respected Advisor
Posts: 3,777

Re: Date Increment

Show the value of FLDATE.

Super User
Posts: 10,500

Re: Date Increment

Can you show us the value of &fldate ?

It may be that you can use the pieces without breaking them up using a different informat like YYMMDD6.

 

 

Frequent Contributor
Frequent Contributor
Posts: 83

Re: Date Increment

The file name is something like data_file_extract_20160219.tar.

 

&fldate = 20160219. I'm using it in another comparison in my program so I figured I could reuse it for what I'm trying to accomplish here. Ultimately I would like to incrememnt it by 1 day. It has to be a date value becuase I'm evaluating it against another date.

Respected Advisor
Posts: 3,777

Re: Date Increment


Ody wrote:

The file name is something like data_file_extract_20160219.tar.

 

&fldate = 20160219. I'm using it in another comparison in my program so I figured I could reuse it for what I'm trying to accomplish here. Ultimately I would like to incrememnt it by 1 day. It has to be a date value becuase I'm evaluating it against another date.


I'm confused by your last statement as to what kind of date you are talking about.  Please provide more details everything and where the comparision of the value of FLDATE+1 will be used.  

Super User
Posts: 10,500

Re: Date Increment

%let newdate =%eval(%sysfunc(inputn(&fldate,yymmdd8.)) + 1);

 

will create a value for newdate that is a SAS date value.

I'm not sure that is what you actually want as you were trying to do something with a DATE9 format.

If you need the result to look like a 20JUN2016

%let newdate =%sysfunc(putn(%eval(%sysfunc(inputn(&fldate,yymmdd8.)) + 1)),date9.));

Solution
‎02-19-2016 03:35 PM
Super User
Super User
Posts: 6,501

Re: Date Increment

[ Edited ]

If you have a macro variable that contains an 8 digit string in the format YYYYMMDD you can convert it to an actual date value using the INPUTN() function which you can call using the %SYSFUNC() macro function.

 

%let date=%sysfunc(inputn(&fldate,yymmdd8));

 

This will be the number of days since 1/1/1960. If you want it formatted in a particular way you can use the optional format specification on the %SYSFUNC() function call.

 

%let datestr=%sysfunc(inputn(&fldate,yymmdd8),date9);

 

 

If you are using the value in an expression in SAS code (like an IF or WHERE statement) then you can use either format, but with the second you need to express it in the form of a date literal.

 

where visdate <= &date ;

where visdate <= &date + 1;

where visdate <= "&datestr"d ;

where visdate <= "&datestr"d+1 ;

 

If you need to generate the next date using the same YYYYMMDD format then use the PUTN() function and the YYMMDDN format.

 

%let nxtdate = %sysfunc(putn(1+%sysfunc(inputn(&fldate,yymmdd8)),yymmddn8));

Frequent Contributor
Frequent Contributor
Posts: 83

Re: Date Increment

[ Edited ]

Many thanks Tom (and Ballardw).

I couldn't quite figure out where to increment the date and your NXTDATE variable was a good example of how I needed to structure the code.

Thank you!

 

edit: In your NXTDATE variable, doesn't the putn change the date back to character or am I missing something?

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 286 views
  • 0 likes
  • 4 in conversation