I have a SAS data set file. One of my columns is an age column that has the age of the subject either as the age in months, weeks, years, or years and month (or missing, which I am treating as years). Is there a SAS format that will help me store the data (in a new column obviously) as a numeric field. I'd like all of the data to show me in years.
Hello and welcome to the SAS communities 🙂
Can you show us what your data looks like? Is there an identifier of whether the age is represented as months , years or years and month?
Its a text field - here's an example of what the data looks like:
17 Years
5 Weeks
7
5 Years 4 Months
3 Months
There are several ways to do this. Is it a requirement to use a format? I do not think there is a standard format available
There is definitely a more efficient way, but here is an approach that works
data have;
input string:$20.;
infile datalines dlm=',';
datalines;
17 Years
5 Weeks
7
5 Years 4 Months
3 Months
;
data want(drop=_: i);
set have;
if (findw(string, 'Years')>0 & countw(string)=2) | anyalpha(string)=0 then do;
age=input(compress(string, , 'kd'), 8.);
end;
else do;
do i=1 to countw(string);
if anyalpha(scan(string, i))=0 then do;
if scan(string, i+1)='Years' then do;
_ageY=input(scan(string, i), 8.);
end;
if scan(string, i+1)='Months' then do;
_ageM=input(scan(string, i), 8.)/12;
end;
if scan(string, i+1)='Weeks' then do;
_ageW=input(scan(string, i), 8.)/52.177457;
end;
end;
end;
age=sum(_ageY, _ageM, _ageW);
end;
format age 8.2;
run;
I doubt that a format already exists. It may be possible to write an appropriate format, but the code will not be pretty either.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.