BookmarkSubscribeRSS Feed
sasuser381
Obsidian | Level 7

I tried to get the first day of current quarter with this code.

 

%let q1=intnx('qtr', today(),0,'b');

%put &q1;

This gives me the result:

 

01JAN1960:06:08:17.000 instead of 01JUN2020:00:00:00

 

9 REPLIES 9
sasuser381
Obsidian | Level 7
Edit:

It gives me the result 01JAN1960:06:08:17
PaigeMiller
Diamond | Level 26

I cannot see how the code you show produces anything that looks like a date or datetime value. You need a %sysfunc or two. Are you sure that is the exact code you are using?

 

This code works for me:

%let q1=%sysfunc(intnx(qtr,%sysfunc(today()),0,b));
%put &=q1;
--
Paige Miller
sasuser381
Obsidian | Level 7
This is the whole code:

%let q1=intnx('qtr', today(),0,'b');
%put &q1;

DATA Q1CHECK;
Q1=&Q1; FORMAT Q1 DATETIME16.;
RUN;
PaigeMiller
Diamond | Level 26

You want to use a date format, not a datetime format.

--
Paige Miller
sasuser381
Obsidian | Level 7
Well i need datetime format actualy as an output. How could i do that? Obviously not this way i did.
Tom
Super User Tom
Super User

@sasuser381 wrote:
Well i need datetime format actualy as an output. How could i do that? Obviously not this way i did.

You need to use datetime values (number of seconds) not date values (number of days) with the DATETIME format. Use the DTQTR interval instead of the QTR interval when working with seconds instead of days.

340   data _null_;
341     now=datetime();
342     qtr=intnx('dtqtr',now,0,'b');
343     put (now qtr) (datetime19.);
344   run;

 24JUL2020:09:49:55 01JUL2020:00:00:00
ghosh
Barite | Level 11

%let q1=%sysfunc(intnx(qtr,%sysfunc(today()),0,b),datetime20.);

/* %let q1=intnx('qtr', today(),0,'b'); */

%put &q1;

smantha
Lapis Lazuli | Level 10

In SAS macro code you need to wrap datastep function in %sysfunc unless you have macro equivalent such as %scan. In your case it will be

%let q1=intnx('qtr', %sysfunc(today()) ,0,'b');

%put &q1;
Reeza
Super User

1. Functions need to be wrapped in %SYSFUNC()

2. For datetime variables the increment needs to start with DT

3. When using functions within macro code, you do not need quotes.

4. TODAY() returns a DATE variable, if you want DATETIME use DATETIME() function instead.

 

%let q1=%sysfunc(intnx(dtqtr, %sysfunc(datetime()), 0, b), datetime.);

%put &q1.;

Results:

 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 68         
 69         %let q1=%sysfunc(intnx(dtqtr, %sysfunc(datetime()), 0, b), datetime.);
 70         
 71         %put &q1.;
 01JUL20:00:00:00
 72         
 73         OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 83         

@sasuser381 wrote:

I tried to get the first day of current quarter with this code.

 

%let q1=intnx('qtr', today(),0,'b');

%put &q1;

This gives me the result:

 

01JAN1960:06:08:17.000 instead of 01JUN2020:00:00:00

 


 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 9 replies
  • 5588 views
  • 3 likes
  • 6 in conversation