SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 2 replies
  • 1225 views
  • 2 likes
  • 2 in conversation