BookmarkSubscribeRSS Feed
BCNAV
Quartz | Level 8

I have set the following global macro var:

 

%let time_1mos_ago = %sysfunc(intnx(month,%sysfunc(today()),-1,end),YYMMDDS8.);

 

the %put of said variable is: TIME_1MOS_AGO=19/09/30

 

This is what I want. I would like to use time_1mos_ago elsewhere to choose dates.

 

I tried:

 

PROC SQL;
   CREATE TABLE EGTASK.QUERY_FOR_OVER_APPEND_0005_0000 AS 
   SELECT t1.billable_id, 
          t1.FltDate, 
          /* year */
            (year(&time_1mos_ago)) AS year
      FROM IBS_COMB.OVER_APPEND t1
      WHERE t1.FltDate <= &time_1mos_ago
      ORDER BY t1.FltDate;
QUIT;

This resulted in empty output....so I removed the restriction to see if the macro variable was passed through properly. I tested this via the above: year(&time_1mos_ago)) AS year

 

Year came back at 1960 when it should have been 2019.  What is missing to ensure that the time_1mos_ago macro variable is passing the date properly so I can use it in many following steps?

2 REPLIES 2
novinosrin
Tourmaline | Level 20

Hi @BCNAV 

 

If you intend to use the date values as number to extract year from date value etc, you are better off not formatting it and just keeping the computed value as a number.

 

So You could use the below to make year function work

%let time_1mos_ago = %sysfunc(intnx(month,%sysfunc(today()),-1,end));

%put &=time_1mos_ago;

1    %let time_1mos_ago = %sysfunc(intnx(month,%sysfunc(today()),-1,end));
2
3    %put &=time_1mos_ago;
TIME_1MOS_AGO=21822

One may get confused  %sysfunc with a format  merely associates a format like a format statement in a datastep does, however in an open code that's not true, where everything is a text(i.e char)  one should be mindful of the fact a result token alphanumeric is always a name token or special token and certainly not a number token at the time compilation done by the word scanner. 

novinosrin
Tourmaline | Level 20

To help further your understanding, your code

 

year(&time_1mos_ago)

 

basically resolves to

 

year(19/09/30)

 

which is 19 divided by 9 divided by 30 and that is equal to 0.0703703704 with integer value being 0. And if you format this, you would get a SAS date value 01jan1960. Taking the year('01jan1960') gives you 1960, which is erroneous. HTH

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 25. 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
  • 2 replies
  • 1045 views
  • 2 likes
  • 2 in conversation