BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Gil_
Quartz | Level 8
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

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

 

View solution in original post

14 REPLIES 14
Reeza
Super User

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

Astounding
PROC Star

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));

 

Gil_
Quartz | Level 8
Correct for example if I do a -5 based on today date bring back 6/ 28/17 ...
Astounding
PROC Star

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.

Gil_
Quartz | Level 8
Astounding sorry I inherited the code it was working when we were in June and now in July it just just brings july data
Gil_
Quartz | Level 8
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...
Gil_
Quartz | Level 8
It seems like the month portion is just looking at current month doesn't allow to go further than july
Gil_
Quartz | Level 8
 
Astounding
PROC Star

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.

Gil_
Quartz | Level 8
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
Reeza
Super User

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...

 

 

Kurt_Bremser
Super User

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.

 

Tom
Super User Tom
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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