DATA Step, Macro, Functions and more

SAS Date Automation, Symput and Intnx

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

SAS Date Automation, Symput and Intnx

Full disclosure, I was trained on SAS EG, and am not a fully fledged programmer.

 

I work for a college, and am in charge of the daily enrollment reports. I am trying to automate these reports using INTNX and SYMPUT, but am stumbling over the date format in SYMPUT.

 

The enrollment reports are compared this year to last, but the dates being compared changes from year to year. The main date change is the 'census' date (census date is when students can no longer drop classes).  The enrollment reports compare 'yesterday' to 'this day last year that is the same number of days from census'. Example I want to compare yesterday 03Mar2016 to last year but there are -7 days difference from the census date. So comparing the info of 03Mar2016 to 29Apr2015.

 

The problem I am having is that the code below runs, without errors, but then when I run the whole program, errors start piling up. I've narrowed the problem to the date format for "Priorname2". In that SYMPUT, date9. will not work, and sasdate. only works when the first part (the code shown) runs, not when the data steps are included.

 

What can I do to get this time last year, -7 days?

 

Here is the code I am using to try and automate my report:

 

options pageno=1 ;

libname COURSE 'K:\Course\SP16\';RUN;
libname COURSE2 'K:\Course\SP15\';RUN;
%LET SEM1 = SP16; RUN;
%LET SEM2 = SP15; RUN;
DATA _NULL_;
      Yesterday1 = INTNX('day',today(),-1,'Same');
      CALL SYMPUT("PriorDateName", PUT(Yesterday1,date9.));
      Last_Year = INTNX('year',today(),-1,'Same');
      CALL SYMPUT("PriorName2", PUT(Last_Year, sasdate.));
	  Last_Yr_Before_Cen = INTNX('day',"&PriorName2",-4,'Same');
      CALL SYMPUT("Prior", PUT(Last_Yr_Before_Cen,date9.));

PUT "&PriorDateName";
PUT "&PriorName2"; /*this is a sas date format, used because date9. format was not recognized by the synput function*/
PUT "&Prior";
RUN;

 

 

Thank you for any input,

Renee


Accepted Solutions
Solution
‎04-04-2016 03:30 PM
Super User
Posts: 5,083

Re: SAS Date Automation, Symput and Intnx

SAS stores dates as integers, with each day being one greater than the day before.  So you should be able to get the date you want more directly:

 

data _null_;

Last_Year = intnx('YEAR', today(), -1, 'Same') - 4;   /* or perhaps -7 if that's what you really want */

 

Then transfer that value in the same DATA step:

 

call symputx('Prior', put(last_year, date9.));

run;

 

The problems may be arising later, because you need to refer to dates with the proper syntax.  This would be one way to use &PRIOR using the proper SAS syntax to refer to a date:

 

"&prior"d

 

It's possible your later code omits the "d" at the end.

View solution in original post


All Replies
Super User
Posts: 10,500

Re: SAS Date Automation, Symput and Intnx

I am not aware of a format SASDATE. so cannot tell what you may be attempting to do with this line:

CALL SYMPUT("PriorName2", PUT(Last_Year, sasdate.));

 

Also it is generally not going to work referencing a macro variable in the same data step that creates it so this would fail in general:

 

  Last_Yr_Before_Cen = INTNX('day',"&PriorName2",-4,'Same');

 

I think you may be looking for:

 

  Last_Yr_Before_Cen = INTNX('day',last_year,-7,'Same');

 

How do you want the values for your macro variables to appear assuming you ran that code on April 4 2016?

 

Occasional Contributor
Posts: 11

Re: SAS Date Automation, Symput and Intnx

Thank you for your help!
Solution
‎04-04-2016 03:30 PM
Super User
Posts: 5,083

Re: SAS Date Automation, Symput and Intnx

SAS stores dates as integers, with each day being one greater than the day before.  So you should be able to get the date you want more directly:

 

data _null_;

Last_Year = intnx('YEAR', today(), -1, 'Same') - 4;   /* or perhaps -7 if that's what you really want */

 

Then transfer that value in the same DATA step:

 

call symputx('Prior', put(last_year, date9.));

run;

 

The problems may be arising later, because you need to refer to dates with the proper syntax.  This would be one way to use &PRIOR using the proper SAS syntax to refer to a date:

 

"&prior"d

 

It's possible your later code omits the "d" at the end.

Occasional Contributor
Posts: 11

Re: SAS Date Automation, Symput and Intnx

Thank you! I was able to drop that into my program and it worked!

New Contributor
Posts: 3

Re: SAS Date Automation, Symput and Intnx

i tend to do this

 

%let prior=%sysfunc(intnx(day,%sysfunc(intnx(year,"&sysdate"d,-1,s)),-4),date9.);

 

"&prior."d

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 749 views
  • 1 like
  • 4 in conversation