BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sirisha
Calcite | Level 5

I have a dataset with age variable. Its coded by 102, 203, 420, 620. The first digit in the code represents the age type-1 represents month, 2 represents days, 4 represents hours and 6 represents minutes.  The second and third digits represent age units, 201- 1 month, 423- 23 hours..... I want to represent age in days. Is there a way to solve this in SAS, what command should I use to get a new variable representing the age in days?

Any kind of help would be appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Assuming you have read in AGE as a number instead of character string.

Looks like you have some trouble with your expected values.  213 would be 13 days.  413 is 13/24 days.

data want ;

   input age expect ;

   select (int(age/100)) ;

     when (1) days=mod(age,100)*30;

     when (2) days=mod(age,100);

     when (4) days=mod(age,100)/24;

     when (6) days=mod(age,100)/(24*60);

     otherwise days=.;

   end;

cards;

101  30

212  12

413  13

114  420

632 0.02

run;

Obs    age    expect       days

1     101     30.00     30.000

2     212     12.00     12.000

3     413     13.00      0.542

4     114    420.00    420.000

5     632      0.02      0.022

View solution in original post

7 REPLIES 7
Reeza
Super User

wouldn't 201 represent 1 day,

So

1 = month

2=day

4=hours

6=minutes

What does your data look like?

Is it the string '102, 203,420, 620' or 4 different variables?

That's easy enough to code but we need more info.

sirisha
Calcite | Level 5

Thank you for responding Reeza. But I need more help

No, 201 represents 1 month and 403 represents 3days.

Each code has 3 digits. The first digit indicates age type such as month, day, hours, minutes.

example,

Age  days

201  30

413  13

214  420

632 0.02

........

7,186 observations.

There are only 6 age types- 2 to 7. 2 for months, 4 for days, 5 for hours, 6 for minutes and 7 unknown. The new column days should be my final output.

Tom
Super User Tom
Super User

Assuming you have read in AGE as a number instead of character string.

Looks like you have some trouble with your expected values.  213 would be 13 days.  413 is 13/24 days.

data want ;

   input age expect ;

   select (int(age/100)) ;

     when (1) days=mod(age,100)*30;

     when (2) days=mod(age,100);

     when (4) days=mod(age,100)/24;

     when (6) days=mod(age,100)/(24*60);

     otherwise days=.;

   end;

cards;

101  30

212  12

413  13

114  420

632 0.02

run;

Obs    age    expect       days

1     101     30.00     30.000

2     212     12.00     12.000

3     413     13.00      0.542

4     114    420.00    420.000

5     632      0.02      0.022

sirisha
Calcite | Level 5

It's working, that's very helpful. Thank you very much every one who worked on this problem.

Linlin
Lapis Lazuli | Level 10

data have;

input age;

cards;

201

413

214

632

;

data want;

  set have;

  _age=put(age,3.);

  if first(_age)='2' then days=30*input(substr(_age,2),2.);

    else if first(_age)='4' then days=input(substr(_age,2),2.);

   else if first(_age)='5' then days=input(substr(_age,2),2.)/24;

     else if first(_age)='6' then days=input(substr(_age,2),2.)/(24*60);

    else days=.;

proc print;run;

art297
Opal | Level 21

Linlin, of course, meant:

     else if first(_age)='6' then days=input(substr(_age,2),2.)/(24*60);

Linlin
Lapis Lazuli | Level 10

Thank Art! I have updated.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 7 replies
  • 2214 views
  • 4 likes
  • 5 in conversation