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
CDOB | Num | 8 | ||||
hepb_date | Char | 5 | $5. | $5. | hepb_date |
but some of the dates look like this
or some look like this
can you help
thanks
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.
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.
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 ;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.