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

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';

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

View solution in original post

14 REPLIES 14
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
hjjijkkl
Pyrite | Level 9
How can i say year/12? I didnt know if it was going to work
else if (time_on_team)='month' then YrsOn='year'/12;
PaigeMiller
Diamond | Level 26

'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

--
Paige Miller
hjjijkkl
Pyrite | Level 9
the variable YearOn has values like ( 4 years, 8month, 6years etc..) and I want the month values to be divided by 12. How can i do that in the if/then statement?
PaigeMiller
Diamond | Level 26

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.

 

 

--
Paige Miller
hjjijkkl
Pyrite | Level 9
my data is very big but, this is how it looks like
ID Time_on_team
1 2years
2 6years
3 6month
4 10years
5 4month
6 8month
I am trying to find the range, mean, sd, IQR median for time_on_team
PaigeMiller
Diamond | Level 26

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."

--
Paige Miller
Tom
Super User Tom
Super User

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

 

hjjijkkl
Pyrite | Level 9
yes
hjjijkkl
Pyrite | Level 9
I want to use an if then stament
like this below. but, the one i created doesnt work
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 YrsOn = 'Years on Team';
Tom
Super User Tom
Super User

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
Reeza
Super User

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';


 

Tom
Super User Tom
Super User

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?

hjjijkkl
Pyrite | Level 9
my data is very big but, this is how it looks like
ID Time_on_team
1 2years
2 6years
3 6month
4 10years
5 4month
6 8month
I am trying to find the range, mean, sd, IQR median for time_on_team

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 14 replies
  • 1271 views
  • 0 likes
  • 4 in conversation