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.
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
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.
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.
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
It's working, that's very helpful. Thank you very much every one who worked on this problem.
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;
Linlin, of course, meant:
else if first(_age)='6' then days=input(substr(_age,2),2.)/(24*60);
Thank Art! I have updated.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.