The SAS Output Delivery System and reporting techniques

How to split codes in SAS

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

How to split codes in SAS

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.


Accepted Solutions
Solution
‎10-18-2012 01:52 PM
Super User
Super User
Posts: 6,370

Re: How to split codes in SAS

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


All Replies
Grand Advisor
Posts: 17,428

Re: How to split codes in SAS

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.

Occasional Contributor
Posts: 5

Re: How to split codes in SAS

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.

Solution
‎10-18-2012 01:52 PM
Super User
Super User
Posts: 6,370

Re: How to split codes in SAS

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

Occasional Contributor
Posts: 5

Re: How to split codes in SAS

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

Super Contributor
Posts: 1,636

Re: How to split codes in SAS

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;

Esteemed Advisor
Posts: 7,299

Re: How to split codes in SAS

Linlin, of course, meant:

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

Super Contributor
Posts: 1,636

Re: How to split codes in SAS

Thank Art! I have updated.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 354 views
  • 4 likes
  • 5 in conversation