BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Banke
Pyrite | Level 9

Hello everyone, 

I have two variables: start_year and end_year. start_year is in recorded in years and in format numeric 8. while end_year is mmddyy10.

i have tried to subtract and get the age but i keep getting funny values even with some of the codes i found.

Attached is the simplest code i tried, your help is highly appreciated.

sample data.

start_date (8.)  end_date(mmddyy10.)

2015                01/02/2017

2013                05/07/2014

2018                06/06/2019

data want;
set have;
format end_date year4.;
end_date2 = end_date;
format end_date2 8.; age = end_date2 - start_date; run;
1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

The calculation

 

age = end_date2 - start_date;

would generate the same result no matter what format you apply to the end_date2 and start_date variables.

 

You need a function to extract the YEAR component of the end_date value (which is numerically the number of days after 01jan1960).  So use

data want;
  set have;
  age = year(end_date) - start_date;
run;

Of course, this will return the same value for the entire 365 (or 366) days that END_DATE might have over the course of a single calendar year.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

The calculation

 

age = end_date2 - start_date;

would generate the same result no matter what format you apply to the end_date2 and start_date variables.

 

You need a function to extract the YEAR component of the end_date value (which is numerically the number of days after 01jan1960).  So use

data want;
  set have;
  age = year(end_date) - start_date;
run;

Of course, this will return the same value for the entire 365 (or 366) days that END_DATE might have over the course of a single calendar year.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Banke
Pyrite | Level 9
Thank you so much!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 393 views
  • 1 like
  • 2 in conversation