SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14

Hello,

What is the way to create a SAS date macro variable with d at the end?

The resulted value in this case should be '21feb2020'd (one day after max date in ttt data set)

 

 

Data ttt;
format date date9.;
input date :date9.;
cards;
21feb2020
31dec2020
04Mar2021
19mar2021
28Jun2021
;
Run;


PROC SQL noprint;
	select  intnx('day',max(date),1)  into: start_date	   
	from  ttt
;
QUIT;
%put &start_date;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

It is not really necessary to use INTNX() to add days since SAS stores dates as number of days.  Just use normal arithmetic.

 

The QUOTE() function can be used to add quotes.


data ttt;
  format date date9.;
  input date :date9.;
cards;
21feb2020
31dec2020
04Mar2021
19mar2021
28Jun2021
;

proc sql noprint;
select max(date)+1 
     , cats(quote(put(max(date)+1,date9.)),'d')
  into :start_date trimmed
     , :start_date_human 
  from  ttt
;
quit;

%put &=start_date &=start_date_human ;
START_DATE=22460 START_DATE_HUMAN="29JUN2021"d

Note that both forms refer to the same day and can be used it most of the same places.  The only restriction is you cannot use the human readable date literal with the %EVAL() macro function.  You would need to use %SYSEVALF() if you want the macro process to see that string as a date.

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

Your solution is already ideal: you store the raw numeric value in the macro variable, which is the most easiest to handle in the following code.

See Maxim 28.

PaigeMiller
Diamond | Level 26

@Ronein wrote:

Hello,

What is the way to create a SAS date macro variable with d at the end?

The resulted value in this case should be '21feb2020'd (one day after max date in ttt data set)

 


Don't create macro variables like this, with quotes and a D on the end. Macro variables should not be formatted or human readable (unless they are needed for titles or labels or filenames, in which case they still don't need the quotes or a D on the end)

--
Paige Miller
Tom
Super User Tom
Super User

It is not really necessary to use INTNX() to add days since SAS stores dates as number of days.  Just use normal arithmetic.

 

The QUOTE() function can be used to add quotes.


data ttt;
  format date date9.;
  input date :date9.;
cards;
21feb2020
31dec2020
04Mar2021
19mar2021
28Jun2021
;

proc sql noprint;
select max(date)+1 
     , cats(quote(put(max(date)+1,date9.)),'d')
  into :start_date trimmed
     , :start_date_human 
  from  ttt
;
quit;

%put &=start_date &=start_date_human ;
START_DATE=22460 START_DATE_HUMAN="29JUN2021"d

Note that both forms refer to the same day and can be used it most of the same places.  The only restriction is you cannot use the human readable date literal with the %EVAL() macro function.  You would need to use %SYSEVALF() if you want the macro process to see that string as a date.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1487 views
  • 1 like
  • 4 in conversation