Macro date not bringing correct date

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 78
Accepted Solution

Macro date not bringing correct date

I have a macro that looks into a daily file with yesterday date .I need to import it which works but if I need to bring last month data it keeps giving me july date here is macro

Option symbolgen ;
%let day2 =%sysfunc (day ( %sysfunc (intnx (day, "&sysdate"d,-3,b))));
Symbolgen: Macro variable sysdate resolves to 03jul17
%let month=.;
%let month2=%sysfunc (month ("&sysdate"d));
Symbolgen : macro variable sysdate resolves to 03jul17
%let month=.;
%let year=% sysfunc (year ("&sysdate"d));
Symbolgen: macro variable sysdate resolves to 03nul17
%let dash=_;
%put &day2 &month2 &year2 ;
Symbolgen: macro variable day2 resolves to 30
Symbolgen: Macro variable month2 resolves to 7
Symbolgen: macro variable year resolve to 2017
30 7 2017
% macro setdates;
%if &day2 <10 %then %let day=0days2.;
%else %let day=&day2.;
%if &month2 <10 %then %let month=0&month2.;
%else %let month=&month2.;
%put &day &month &year ;
%mend;
I need it to pull data from June it keeps bringing july data


Accepted Solutions
Solution
‎07-05-2017 12:29 PM
Super User
Posts: 7,782

Re: Macro date not bringing correct date

You never got June in your month because you set it once from &sysdate, but did not manipulate it the way you did with &day2.

And you have lots of confusion regarding day, day2, month, month2, year, year2.

Basically, that code is a total abomination. Discard it completely and rewrite it from scratch, according to what you need in the end.

 

So, to get year, month and day, I'd do this:

data _null_;
todays_date = today() - 5;
day = day(todays_date);
month = month(todays_date);
year = year(todays_date);
call symput('day',put(day,z2.));
call symput('month',put(month,z2.));
call symput('year',put(year,z4.));
run;
%put &day &month &year;

Note that you don't need the intnx function if you just want to subtract days.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 19,792

Re: Macro date not bringing correct date

What do you want as final output? The day/month/year for last month?

Super User
Posts: 5,504

Re: Macro date not bringing correct date

You're making something that is relatively easy appear to be incredibly difficult.  Why not start with this:

 

%let cutoff_date = %eval("&sysdate9"d - 3);

 

If you need to (and there is no guarantee that you need to do this ... you haven't shown us what follows to extract data):

 

%let cutoff_date = %sysfunc(putn(&cutoff_date, date9));

 

You could use this instead, but it's not very useful for selecting observations in subsequent steps:

 

%let cutoff_date = %sysfunc(putn(&cutoff_date, yymmdd10));

 

Frequent Contributor
Posts: 78

Re: Macro date not bringing correct date

Correct for example if I do a -5 based on today date bring back 6/ 28/17 ...
Super User
Posts: 5,504

Re: Macro date not bringing correct date

[ Edited ]

You may have to explain this in words.  If the program runs today, what date would you like it to return?  You had hard-coded -3 in your original post.

 

When it comes to selecting data, you will have to show the portion of your code that uses the results to select data as well.  This section just creates macro variables that are used to select data at a later (unknown to us) point.

Frequent Contributor
Posts: 78

Re: Macro date not bringing correct date

Astounding sorry I inherited the code it was working when we were in June and now in July it just just brings july data
Frequent Contributor
Posts: 78

Re: Macro date not bringing correct date

The script runs daily -1 I noticed I was missing dates in June so I counted backwards to get dated I need --5,-7-9...
Frequent Contributor
Posts: 78

Re: Macro date not bringing correct date

It seems like the month portion is just looking at current month doesn't allow to go further than july
Frequent Contributor
Posts: 78

Re: Macro date not bringing correct date

 
Super User
Posts: 5,504

Re: Macro date not bringing correct date

You will have to explain a little more.  For example:

 

"If I run the program on any day during July, I want it to return a date that is the last date in June."

 

If that is what you want, it's easy enough to do:

 

%let cutoff_date = %sysfunc(intnx(month, "&sysdate9"d, -1, e));

 

But you haven't yet described if that is what you need, and you absolutely need to show the later code that uses this date because the format of &CUTOFF_DATE will have to be adjusted to fit the format expected by the program.

Frequent Contributor
Posts: 78

Re: Macro date not bringing correct date

Normally it will run -1 for yesterday date ... so if I need to back track 5 days I would pull by change day -1 To -5 To get that date.i will try your suggestion see if that works once it id the correct date it looks for it in the directory for that date and imports in file with the date i select -1 or-5 that works...its id the correct date
Super User
Posts: 19,792

Re: Macro date not bringing correct date

SYSDATE is the date which your session started running. You may want to use the DATE() function to return todays date. 

 

>Contains the date on which a SAS job or session began executing.

http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#n08zfkx3lmiagpn11...

 

 

Solution
‎07-05-2017 12:29 PM
Super User
Posts: 7,782

Re: Macro date not bringing correct date

You never got June in your month because you set it once from &sysdate, but did not manipulate it the way you did with &day2.

And you have lots of confusion regarding day, day2, month, month2, year, year2.

Basically, that code is a total abomination. Discard it completely and rewrite it from scratch, according to what you need in the end.

 

So, to get year, month and day, I'd do this:

data _null_;
todays_date = today() - 5;
day = day(todays_date);
month = month(todays_date);
year = year(todays_date);
call symput('day',put(day,z2.));
call symput('month',put(month,z2.));
call symput('year',put(year,z4.));
run;
%put &day &month &year;

Note that you don't need the intnx function if you just want to subtract days.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 7,782

Re: Macro date not bringing correct date

Oh yeah, and use the {i} button to post a log. The {i} subwindow uses a fixed font and preserves all original formatting and characters.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,046

Re: Macro date not bringing correct date

[ Edited ]

You are calculating the MONTH and YEAR from the wrong date.  Also no need for macro logic to get zero padded numbers, just use a format when you generate the macro variable.

%let mydate=%sysfunc(intnx(day,"&sysdate9"d,-3),date9);
%let day=%sysfunc(day("&mydate"d),z2);
%let month=%sysfunc(month("&mydate"d),z2);
%let year=%sysfunc(year("&mydate"d),z4);
%put &=sysdate9 &=mydate &=day &=month &=year ;
SYSDATE9=02JUL2017 MYDATE=29JUN2017 DAY=29 MONTH=06 YEAR=2017
☑ This topic is solved.

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

Discussion stats
  • 14 replies
  • 254 views
  • 0 likes
  • 5 in conversation