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-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
  • 6 replies
  • 668 views
  • 2 likes
  • 2 in conversation