BookmarkSubscribeRSS Feed
rg529
Calcite | Level 5


DATA all_age;
SET all;
BY subject dov;
RETAIN subject;
IF first.dov THEN age=(dov-dob)/365.25;
RUN;

 

Hi, can someone help me figure out how to only calculate age using only the first date of visit from the first subject as there are multiple outputs per subject. And applying that age to the rest of the data for the same subject? Just a student pulling out some hair... help plz!

7 REPLIES 7
novinosrin
Tourmaline | Level 20

Based on your description,

 

my understanding is

 

DATA all_age;
SET all;
BY subject dov;
RETAIN age;
IF first.dov THEN age=(dov-dob)/365.25;
RUN;

rg529
Calcite | Level 5
all dates are in SAS format, and the below code works, THANK YOU.
 
DATA all_age;
SET all;
BY subject;
RETAIN age;
IF first.subject THEN age=YRDIF(dob, dov, 'actual');
RUN;
 
BUT getting this error message in log?
 
NOTE: Invalid argument to function YRDIF(.,14193,'actual') at line 77 column 27.
subject=102 dov=14193 dbp=73 sbp=128 site=1 gender=F dob=. doe=14193 smoke=0 drink=4 marital=3 education=3 glucose=119 height=62
weight=160 hdl=54 ldl=151 tg=133 tc=238 chd=1 pregnancy=0 exercise=0 FIRST.subject=1 LAST.subject=0 age=. _ERROR_=1 _N_=285

 

also for some reason using the dov-dob/365 is not working with code above

novinosrin
Tourmaline | Level 20

You can't have missing values as an argument to yrdif as log explains YRDIF(.,14193,'actual')

 

If you want more help, please post  a good representative sample of what you have

Reeza
Super User

@rg529 wrote:
all dates are in SAS format, and the below code works, THANK YOU.
 
DATA all_age;
SET all;
BY subject;
RETAIN age;
IF first.subject THEN age=YRDIF(dob, dov, 'actual');
RUN;
 
BUT getting this error message in log?
 
NOTE: Invalid argument to function YRDIF(.,14193,'actual') at line 77 column 27.
subject=102 dov=14193 dbp=73 sbp=128 site=1 gender=F dob=. doe=14193 smoke=0 drink=4 marital=3 education=3 glucose=119 height=62
weight=160 hdl=54 ldl=151 tg=133 tc=238 chd=1 pregnancy=0 exercise=0 FIRST.subject=1 LAST.subject=0 age=. _ERROR_=1 _N_=285

 

also for some reason using the dov-dob/365 is not working with code above


In that particular record, your dob is missing, so it cannot calculate an age.

 

if not missing(dob) then age = ... 

 

Add an IF statement to your code to only calculate age when DOB is not missing.

FreelanceReinh
Jade | Level 19

 

@Reeza wrote:


Add an IF statement to your code to only calculate age when DOB is not missing.


For example, you could introduce the IF condition n(dob, dov)=2, which means "dob and dov are not missing":

 

if first.subject then do;
  if n(dob, dov)=2 then age= (your age formula here);
  else age=.;
end;

Are you sure you want 'actual' as the third argument of YRDIF given that 'age' is an option designed for calculating a person's age?

 

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

Please post sample data ?

It is unclear what format your dov and dob are in are they SAS dates ?

 

 

rg529
Calcite | Level 5
yes in sas dates sorry let me post

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 1141 views
  • 3 likes
  • 5 in conversation