BookmarkSubscribeRSS Feed
Cooksam13
Fluorite | Level 6

I need to find the age at a test was given. 

CBDOB is the birth date

and hepb_date is the date the test was given

 

  Variable Type Len Format Informat Label 

CDOBNum8   
 hepb_dateChar5$5.$5.hepb_date

 

but some of the dates look like this

Cooksam13_0-1651078123453.png

or some look like this 

Cooksam13_1-1651078277597.png

can you help

 

thanks

4 REPLIES 4
Tom
Super User Tom
Super User

Looks like imported data from an Excel spreadsheet.  One that had a column where some of the cells where character strings and some where dates.

 

To convert that strings into dates first convert them into numbers, then adjust the number for difference in how Excel and SAS count days.

data want;
  set have ;
  if indexc(hepb_date,'/') then year=input(hepb_date,4.);
  else  date = input(hepb_date,??32.)
  if not missing(date) then do;
    date=date+'30DEC1899'd ;
    year=year(date);
  end;
  format date date9.;
run;

Once you have numeric DATE values or at least numeric YEAR value now you can try to calculate an AGE.

Is HEBP_DATE the day that you want to know the age for?

 Do you only have year of birth?

  Without full dates for both ends of the interval you can only get an estimate of the age.

Cooksam13
Fluorite | Level 6
yes the hepb_date is the date i want to know the age, and I only have the year of birth
Cooksam13
Fluorite | Level 6

now Im having trouble with the other side of things

I am still trying to find the age from hepb_date and the CDOB date 

but the CDOB is in two different formats and i don't know how to universalize them, all i need is the year because I can use the year from the new hepb_date variable that is now "year" as you ( @Tom  )suggested.

Cooksam13_0-1651173216104.png

 

 

Tom
Super User Tom
Super User

It is hard to tell what you actually have in the data from looking a PHOTOGRAPH of the data.

Is CDOB also character?
Then use the SAME METHOD to convert it to a number.  Just adjust for the style of the strings.

data want;
  set have;
  if index(cdob,'/') then do;
     dob=input(cdob,mmddyy10.);
     dobyr=year(dob);
  end;
  else dobyr=input(cdob,4.);
run; 

Now if you have both DOBYR and YEAR as numbers to find an estimate of the age just subtract them.

age = year - dobyr ;

 

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!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 416 views
  • 0 likes
  • 2 in conversation