I have a variable "time_on_team" that has both year and month values and I want to format the variable and say the month is year/12 and I want to put around it to 2 decimal place. How do you around the year/12 to two decimal place?
length Yrson 8.;
if (time_on_Team)= 'year' then YrsOn= 'Year';
else if (time_on_team)='month' then YrsOn='year'/12;
drop time_on_team;
label YrsOnARV = 'Years on ARVs';
First parse the string into two parts. The digits and they units. Convert the digits into a number. Then based on whether the units indicate months divide the number by 12.
So assuming you have only the two forms and the values have a space it is really easy.
data have;
id+1;
input time_on_team $20.;
cards;
2 years
6 years
6 month
10 years
4 month
8 month
;
data want;
set have;
duration=input(scan(time_on_team,1,' '),32.);
if index(time_on_team,'month') then duration=duration/12;
run;
Results:
time_on_ Obs id team duration 1 1 2 years 2.0000 2 2 6 years 6.0000 3 3 6 month 0.5000 4 4 10 years 10.0000 5 5 4 month 0.3333 6 6 8 month 0.6667
You can use the ROUND function.
However, your code can't possibly work, dividing a character string by 12. Get as much of the code to work before worrying about rounding.
'year' is a character string, which cannot be divided by 12
year is (possibly, I don't know your data) a variable name, and if this variable is numeric, it can be divided by 12
As stated above by @Tom , please show us a portion of your data. Please follow these instructions and do not show us your data via any other method.
Showing a portion of the data is fine. However, the specific request was "Please follow these instructions and do not show us your data via any other method."
So you want to convert strings into numbers? How consistent are the strings? Are they as messy as your examples?
4 years 8month 6years
Can we assume there is always a space between the digits and the letters?
4 years 8 month 6 years
First parse the string into two parts. The digits and they units. Convert the digits into a number. Then based on whether the units indicate months divide the number by 12.
So assuming you have only the two forms and the values have a space it is really easy.
data have;
id+1;
input time_on_team $20.;
cards;
2 years
6 years
6 month
10 years
4 month
8 month
;
data want;
set have;
duration=input(scan(time_on_team,1,' '),32.);
if index(time_on_team,'month') then duration=duration/12;
run;
Results:
time_on_ Obs id team duration 1 1 2 years 2.0000 2 2 6 years 6.0000 3 3 6 month 0.5000 4 4 10 years 10.0000 5 5 4 month 0.3333 6 6 8 month 0.6667
This answers your question
YrsOn = round(YrsOn, 0.01);
format YrsOn 8.2;
But I don't think this is correct: YRSON would now be a character variable, not numeric as you've assigned it the value "Year" and I would assume your ELSE line of code would generate an error. Are you trying to access a variable called year? If so, remove the quotes from around the name as that's a character string, not a variable name.
length Yrson 8.;
if (time_on_Team)= 'year' then YrsOn= 'Year';
else if (time_on_team)='month' then YrsOn='year'/12;
If you have a variable year you could also refer to it as 'Year'n but if the n does not follow the quote then it will be interpreted as a character string, not a variable name. I don't think this is your issue however.
@hjjijkkl wrote:
I have a variable "time_on_team" that has both year and month values and I want to format the variable and say the month is year/12 and I want to put around it to 2 decimal place. How do you around the year/12 to two decimal place?
length Yrson 8.;
if (time_on_Team)= 'year' then YrsOn= 'Year';
else if (time_on_team)='month' then YrsOn='year'/12;
drop time_on_team;
label YrsOnARV = 'Years on ARVs';
Please show an example of your data.
Does the variable TIME_ON_TEAM have the strings YEAR and MONTH? Or does it have a number?
If it has a number then how can you tell if the number is a number of months or number of years?
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.