BookmarkSubscribeRSS Feed
Q1983
Lapis Lazuli | Level 10

I began with the following data _null_ to attempt to get todays date in yymmdd10  (ie '2020-02-16')

DATA _NULL_;

CALL SYMPUTx('DEKEY', "'"|| PUT(INTNX('DAY',&sysdate9,-0,'END'),yymmddd10.) ||"'") ;

RUN;

%put &dekey;

 

I am attempting to get the first and last day of last year using the sysdate function

(ie '2019-01-01'  and then a separate data _null for '2019-12-31')

5 REPLIES 5
PaigeMiller
Diamond | Level 26

It would be easier using the TODAY() function

 

data _null_;
    call symputx('begin',intnx('year',today(),0,'b');
    call symputx('end',intnx('year',today(),0,'e');
run;
--
Paige Miller
novinosrin
Tourmaline | Level 20

Hi @Q1983  If you are referring to SYSDATE automatic macro variable as a date from today, I am afraid you may get unexpected results as sysdate contains the date on which a SAS job or session began executing and not really today's date.

 

So if the above seems to make sense, you can use Today() function

 

DATA _NULL_;

CALL SYMPUTx('last_date',put(intnx('year',today(),-1,'e'),yymmdd10.),'g') ;
CALL SYMPUTx('first_date',put(intnx('year',today(),-1,'b'),yymmdd10.),'g') ;

RUN;

%put &=last_date;
%put &=first_date;
LOG:

1476  %put &=last_date;
LAST_DATE=2019-12-31
1477  %put &=first_date;
FIRST_DATE=2019-01-01

 

 

PaigeMiller
Diamond | Level 26

Comment: it usually is not necessary to format a macro variable, because to make use of it (except in titles or labels), you will have to un-format it later. So just leave it unformatted.

--
Paige Miller
novinosrin
Tourmaline | Level 20

Fully agree. I did think of it, and I did un-think of it to offer just what the OP mentioned.  🙂

Tom
Super User Tom
Super User

The is no SYSDATE function. There is a SYSDATE macro variable (But it is much better to use the SYSDATE9 macro variable instead since it has all four digits of the year.) If you want to use the SYSDATE9 macro variable as a date then you need to convert it to an actual date by use date literal syntax.

"&sysdate9"d

Note that SYSDATE9 macro variable is set when your SAS session starts.  If you want the current date then use the DATE() function instead, or its alias TODAY().

If you want to move the first (or last) day of the year use the YEAR interval.(NOTE  It is not necessary to use the DAY interval for dates since they are already stored as a count of days, you can just use math.) 

Or you could just find the YEAR and use MDY() with 1,1 or 12,31 for the M and D .

data _null_;
  now=date();
  first_day = mdy(1,1,year(now));
  last_day= mdy(12,31,year(now));
  call symputx('first_day',quote(put(first_day,yymmdd10.),"'"));
  call symputx('last_day',quote(put(last_day,yymmdd10.),"'"));
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1439 views
  • 4 likes
  • 4 in conversation