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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 1276 views
  • 1 like
  • 4 in conversation