DATA Step, Macro, Functions and more

Multiple conditions on renaming observations

Accepted Solution Solved
Reply
Contributor
Posts: 65
Accepted Solution

Multiple conditions on renaming observations

I am wondering if there is any very clever way of renaming the observations in a variable according to the frequency they appear, it has been a bit of a brain teaser for me today.

 

Imagine having the dataset

 

data have;


set have;


input ID Business $;


datalines;

1 Retail
2 Corporate
3 Retail
4 Retail
5 Retail
6 Corporate
7 SME
8 Retail
9 Retail
10 Corporate
;
run;

 

is there any way that you can change observations in the business variable if there is less than 10% representation in the variable

I.e. in a proc freq - variable SME would account for 10%, would there possibly be a code to say

 

data want;
set have;

if business(Obs)<10% then business="Other";

run;

 

it's sounding a little far fetched but if this is not possible, my question is can I have multiple constraints on the statement below:

 

data want;
set have;

if Business^= "Retail" or "Corporate"
then Business = "Other";

run;

Accepted Solutions
Solution
‎03-14-2018 01:35 PM
Super User
Posts: 23,771

Re: Multiple conditions on renaming observations

What about a custom format?

 

data have;
input ID Business $15.;
datalines;
1 Retail
2 Corporate
3 Retail
4 Retail
5 Retail
6 Corporate
7 SME
8 Retail
9 Retail
10 Corporate
;
run;

proc format;
value $ business_type_fmt
'Retail' = 'Retail'
'Corporate' = 'Corporate'
other = 'Other';
run;

proc freq data=have;
table business;
format business $business_type_fmt.;
run;

View solution in original post


All Replies
Solution
‎03-14-2018 01:35 PM
Super User
Posts: 23,771

Re: Multiple conditions on renaming observations

What about a custom format?

 

data have;
input ID Business $15.;
datalines;
1 Retail
2 Corporate
3 Retail
4 Retail
5 Retail
6 Corporate
7 SME
8 Retail
9 Retail
10 Corporate
;
run;

proc format;
value $ business_type_fmt
'Retail' = 'Retail'
'Corporate' = 'Corporate'
other = 'Other';
run;

proc freq data=have;
table business;
format business $business_type_fmt.;
run;
Highlighted
Contributor
Posts: 65

Re: Multiple conditions on renaming observations

Most likely the best way, I was unsure how format can be used for an umbrella "other" term thank you

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 87 views
  • 4 likes
  • 2 in conversation