DATA Step, Macro, Functions and more

Creating an integer age

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 121
Accepted Solution

Creating an integer age

Hi!  I have the following syntax, but the PATAGE variable I created is just spitting out "." in all value slots.  Can someone please tell me where my error is?  Thanks!!

 

data want;

set have;

keep admdate admdx DX1-DX20 marital pasth bencatx patdob dds dispdate dmisbenf dmissex ethnic pasth patcat patname paygrade prvadm race patage recage;

format dispdate mmddyy10.;

format admdate mmddyy10.;

format birtdate mmddyy10.;

patdob =birtdate;

format patdob mmddyy10.;

patage= Int(yrdif(N_patdob,N_admdate,'actual'));

run;


Accepted Solutions
Solution
‎11-23-2015 01:27 PM
Trusted Advisor
Posts: 1,115

Re: Creating an integer age

[ Edited ]

Just one more piece of advice: It's always helpful for yourself and for people who read or work with your programs to keep the code consistent, even in cases where this consistency is not necessary for proper functioning.

 

  • You bother to format variable BIRTDATE nicely, although this variable is not kept (hence dropped) anyway, so that the assigned format will never be effective.
  • It looks like variable PATDOB takes over from BIRTDATE. If it's just a change of names, you can use a RENAME statement (or RENAME= dataset option) to achieve this more easily.
  • It's a bit odd that neither the just created numeric date variable PATDOB (patient's date of birth, I guess) is used in the calculation of PATAGE (presumably patient's age), nor the numeric date variable ADMDATE. Instead, you use two seemingly similar variables N_PATDOB and N_ADMDATE. Does the "N_" prefix, by any chance, stand for "numeric", i.e. SAS date value as opposed to a date in a human-readable format? If so, what is the difference to PATDOB and ADMDATE, then?

View solution in original post


All Replies
Respected Advisor
Posts: 3,902

Re: Creating an integer age

[ Edited ]

For us to really know what's going on you need to provide sample data for "have".

 

What is the SAS log telling you? Any warnings or notes about unititialized variables or missing values?

 

Just by looking into your code:

- N_patdob and N_admdate are variables sourced from "have". What are their values? If any of the two is missing then the result will be missing.

-  Instead of Int(yrdif(N_patdob,N_admdate,'actual')) use intck('year',N_patdob,N_admdate,'c')  

Contributor
Posts: 55

Re: Creating an integer age

Without seeing sample data it's hard to say exactly what's wrong. Try adding n_patdob , n_admdate to your keep statement to make sure they have values and are valid numerics (SAS dates presumably).

 

If you could post some sample of your data, masked if it's a matter of P.H.I or P.I.I. Your approach seems fine though syntactically as this works..

 


data _null_;
   n_patdob='10APR1988'd;
   n_admdate='20NOV2015'd;
   int_age_act=int(yrdif(n_patdob, n_admdate, 'actual'));
   put int_age_act= ;
run;
Respected Advisor
Posts: 4,659

Re: Creating an integer age

The third parameter of yrdif should be "age". If you don't get error messages it may be that that part of the code is never executed. All attempts I have made to generate missing values resulted in error messages. This is what I tried:

 

data _null_;
/* Proper dates */
patdobD = '15JAN2000'd; 
patadmD = '15JAN2015'd; 
/* Non integer dates */
patdobDF = '15JAN2000'd + 0.4; 
patadmDF = '15JAN2015'd + 0.6; 
/* Datetimes instead of dates */
patdobDT = '15JAN2000:00:00:00'dt; 
patadmDT = '15JAN2015:00:00:00'dt; 
format patdobD patadmD patdobDF patadmDF date9. 
    patdobDT patadmDT datetime.;
do type = "actual", "age";
    /* Age calculation with proper dates */
    ageD = yrdif(patdobD, patadmD, type);
    /* Age calculation with fractional dates */
    ageDF = yrdif(patdobDF, patadmDF, type);
    /* Age calculation with datetimes */
    ageDT = yrdif(patdobDT, patadmDT, type);
    /* Age calculation with missing value */
    ageMiss = yrdif(patdobD, ., type);
    /* Age calculation with dates in reversed order*/
    ageReverse = yrdif(patadmD, patdobD, type);
    put (_all_) (=/) /;
    end;
run;
PG
Solution
‎11-23-2015 01:27 PM
Trusted Advisor
Posts: 1,115

Re: Creating an integer age

[ Edited ]

Just one more piece of advice: It's always helpful for yourself and for people who read or work with your programs to keep the code consistent, even in cases where this consistency is not necessary for proper functioning.

 

  • You bother to format variable BIRTDATE nicely, although this variable is not kept (hence dropped) anyway, so that the assigned format will never be effective.
  • It looks like variable PATDOB takes over from BIRTDATE. If it's just a change of names, you can use a RENAME statement (or RENAME= dataset option) to achieve this more easily.
  • It's a bit odd that neither the just created numeric date variable PATDOB (patient's date of birth, I guess) is used in the calculation of PATAGE (presumably patient's age), nor the numeric date variable ADMDATE. Instead, you use two seemingly similar variables N_PATDOB and N_ADMDATE. Does the "N_" prefix, by any chance, stand for "numeric", i.e. SAS date value as opposed to a date in a human-readable format? If so, what is the difference to PATDOB and ADMDATE, then?
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 250 views
  • 1 like
  • 5 in conversation