BookmarkSubscribeRSS Feed
Son_Of_Krypton
Fluorite | Level 6

I have variable named date with value 

date=2020

 

and i want to get the previous year based on the value in the date column i.e 2019.

 

how do i get that.??

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

This does noot seem to be a date varaible? Rather a year variable?

 

If so, then simply subtract 1:

 

data _null_;
   date1 = 2020;
   date2 = date1 - 1;

   put date1 = / date2 =;
run;
Kurt_Bremser
Super User

@Son_Of_Krypton wrote:

I have variable named date with value 

date=2020

 

and i want to get the previous year based on the value in the date column i.e 2019.

 

how do i get that.??


date - 1
novinosrin
Tourmaline | Level 20

Hi @Son_Of_Krypton  Are you seriously asking a subtraction question? Or are you trying to mean previous as LAG, as in previous value read in queue from the previous record in a dataset? 

Son_Of_Krypton
Fluorite | Level 6
no i am having date like 09NOV2020 and then i extracted year from it i.e 2020 based on this year i want previous year in another variable

I know that it can be done via intnx on date 09NOV2020 like

Test=intnx('Year','09NOV2020'd,-1);
Test2=Year(Test);

but is it possible to do on year 2020 only.??
novinosrin
Tourmaline | Level 20

Right, I am afraid I am still not clear with your objective.

You mentioned you have a date variable with a value 09NOV2020

 

Extracting the YEAR value from the date value would indeed give you 2020. And then just like other's suggestion it should be year(date)-1 however this will give 2019. So when your "year" is already 2020, I would think you do not need to subtract unless you want 2019. 

 

Am i mistaken, or is it the damn starbucks coffee not strong enough?hmm

Reeza
Super User
if year(dateVariable) = 2020 then Test=intnx('Year', dateVariable, -1);

Replace the dateVariable with the name of your variable and you're fine.

 


@Son_Of_Krypton wrote:
no i am having date like 09NOV2020 and then i extracted year from it i.e 2020 based on this year i want previous year in another variable

I know that it can be done via intnx on date 09NOV2020 like

Test=intnx('Year','09NOV2020'd,-1);
Test2=Year(Test);

but is it possible to do on year 2020 only.??

 

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
  • 7 replies
  • 3597 views
  • 0 likes
  • 5 in conversation