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

I'm trying to find the quartiles for a variable in my data set, then create a new variable that takes different values depending on the quartile. 

4 if the percentage for  var1 is greater than the third quartile
3 if the percentage for  var1 is between the median & the third quartile
2 if the percentage for  var1 is between the first quartile & the median
1 if the percentage for  var1 is lower than the first quartile
. ( missing value) if the percentage is not given
Format the new_var to name the groups “high”, “med-high”, “med-low”, and “low”, respectively. Add a label to the variable as well.

 

my data looks like this:

data mydata;

input var1 income location percentage; 

1 500 ny 100%

2 400 nj 90%

3 400 ma 80%

4 800 va 80%

5 500 ca 75%

6 600 ma 70%

7 900 wv 60%

8 1000 ny 50%

9 100 pa 40%

10 760 wy 20%

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

PROC RANK will obtain the quartiles (although it will produce values 0 to 3 instead of 1 to 4, I leave that conversion up to you as a homework assignment, although its totally unnecessary to do given that you are going to assign words like "high" to these values).

 

proc rank data=mydata groups=4 out=want;
    var var1;
    ranks new_var;
run;

You can create a custom format to assign the words like "high" to your quartiles. Example of creating a custom format: 

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.4&docsetId=proc&docsetTarget=n03qskw...

 

Now, @jude1 , we have a request so you can help us out in the future. Please provide your data with WORKING SAS code, not the code you provided which doesn't exactly work. That will help you get better and quicker answers.

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

PROC RANK will obtain the quartiles (although it will produce values 0 to 3 instead of 1 to 4, I leave that conversion up to you as a homework assignment, although its totally unnecessary to do given that you are going to assign words like "high" to these values).

 

proc rank data=mydata groups=4 out=want;
    var var1;
    ranks new_var;
run;

You can create a custom format to assign the words like "high" to your quartiles. Example of creating a custom format: 

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.4&docsetId=proc&docsetTarget=n03qskw...

 

Now, @jude1 , we have a request so you can help us out in the future. Please provide your data with WORKING SAS code, not the code you provided which doesn't exactly work. That will help you get better and quicker answers.

--
Paige Miller
jude1
Fluorite | Level 6
Thanks for your help. I should have asked my question better. I know how to use proc format with a dataset, but combining it with other procs is what i do not know how to do. I tried applying it with the proc ranks above and i could not figure it out
PaigeMiller
Diamond | Level 26

When you try to use data set WANT, apply the format then.

--
Paige Miller

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 2112 views
  • 1 like
  • 2 in conversation