BookmarkSubscribeRSS Feed
Ariel_1
Fluorite | Level 6

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.  

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

 

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?

Ariel_1
Fluorite | Level 6

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

PeterClemmensen
Tourmaline | Level 20

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

PeterClemmensen
Tourmaline | Level 20

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;
Ariel_1
Fluorite | Level 6
Thanks for the code! I was hoping that there was a number format in sas
that stored either years or months or weeks or combinations. This is
very helpful!
PeterClemmensen
Tourmaline | Level 20

I doubt that a format already exists. It may be possible to write an appropriate format, but the code will not be pretty either.

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 999 views
  • 2 likes
  • 2 in conversation