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

Hi!

 

I need some help. I have a dataset with the follow structure(the are not the real name is only for exemple) and i want to order (descending) the Vble_B for each differnt value of Vble_A.

 

For exemple:

I have this:

Another variable 1Another variable 2Another variable 3Vble_AVle_BAnother variable 4Another variable 5Another variable 6
   XY0,6   
   XY0,3   
   XY0,4   
   XY0,12   
   XY0,56   
   BV0,18   
   BV0,56   
   BV1,2   
   BV0,32   
   BV0,8   

 

And I want to get this :

 

Another variable 1Another variable 2Another variable 3Vble_AVle_BAnother variable 4Another variable 5Another variable 6
   BV0,18   
   BV0,32   
   BV0,56   
   BV0,8   
   BV1,2   
   XY0,12   
   XY0,3   
   XY0,4   
   XY0,56   
   XY0,6   

 

I need do this to can choose then the higher 10%.

 

Do you have any idea to make that ? I tried with PROC sort and PROC sql/order by I can't get the right result.

 

Thank you very much!!

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

OK. I understand what you mean now .

 

%let percent=0.1;

data class;
 set sashelp.class;
run;

proc summary data=class nway;
class sex;
var weight;
output out=n(drop=_:) n=freq;
run;
data n;
 set n;
 n=ceil(&percent*freq);
 drop freq;
run;
proc sort data=class;
by sex descending weight;
run;
data want;
 merge class n;
 by sex;
 if first.sex then count=0;
 count+1;
 if count le n;
 drop count n;
run;

View solution in original post

13 REPLIES 13
Quentin
Super User

Please show what you tried, and describe the problem with the result you got.

 

Did you try:

proc sort data=have;
  by Var_A Var_B;
run;

?

 

It looks to me like that should work, assuming you want an alphabetical sort order.  If not, there are other sorting options.

BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
luciacossaro
Obsidian | Level 7

I can do it; thank you. Now i have problems to choose the 10% higher  for each Vble_A's value. I tried that but it make 10 groups for all the varaibles and not for the value into each value of Vble_A. Do you have any idea ? (Sorry, i am a new sas user).

 

PROC rank DATA=SUP GROUPS=10 DESCENDING OUT=DIX_POURC_SUP;
BY CLE_UNIQUE ;
VAR RAPPORT
RANKS DECILE;
RUN;

 

Thank you very much!!

SuryaKiran
Meteorite | Level 14

You might try something like this

data have;
input 	Vble_A:$	Vle_B:$;
datalines;
XY 0,6
XY 0,3
XY 0,4
XY 0,12
XY 0,56
BV 0,18
BV 0,56
BV 1,2
BV 0,32
BV 0,8
;
run;

proc sql;
create table want as 
select *,INPUT(scan(Vle_B,1,','),3.) as one,INPUT(substr(scan(Vle_B,2,','),1,1),3.) as two from have
order by Vble_A,one,two
;
run;
Thanks,
Suryakiran
Astounding
PROC Star

To do this easily, you need a little more control over your data.  In this case, when SAS sees "0,8" it has to decide whether that means "0,80" or whether it means "0,08".  SAS actually uses the wrong interpretation (wrong for your purposes) and interprets "0,8" as "0,08".  If you are able to change the data to insert the trailing "0" where appropriate, you could sort your data using:

 

proc sort  data=mydata sortseq=linguistic(Numeric_Collation=ON);

by vble_a vble_b;

run;

Ksharp
Super User
proc sort data=sashelp.heart out=heart;
 by sex;
run;

proc rank data=heart out=temp groups=10 descending;
by sex;
var weight;
ranks rank;
run;

data want;
 set temp;
 if rank=0;
run;
luciacossaro
Obsidian | Level 7

Thank you for your advice. I tried that but it makes 10 decile for the variables in all of dataset (independiently of CLE_UNIQUE's value) and i need to get 10 decile for each value of CLE_UNIQUE.

 

I did that :

 

PROC rank DATA=SUP GROUPS=10 DESCENDING OUT=DIX_POURC_SUP;
BY CLE_UNIQUE ;
VAR RAPPORT;
RANKS DECILE;
RUN;

 

Do you know how this don't work ?

 

Thank you!

Ksharp
Super User

The code should work.

 

 

proc sort data=sashelp.class out=heart;
 by sex;
run;

proc rank data=heart out=temp groups=3 descending;
by sex;
var weight;
ranks rank;
run;

proc freq data=temp;
table sex*rank/list;
run;

 

 

Check OUTPUT.

 

luciacossaro
Obsidian | Level 7

It don't work, I believe the proble is the number of group is biger than the number of observations in some Cle_unique's value.

If you try this code, it don't work. That is the problem with my dataset. I need to class observations to can choose 10% higher in each value of Cle_unique but sometimes this variable have 60 observation and sometime only 4.

 

Thank you for your advice.

 

proc sort data=sashelp.class out=heart;
 by sex;
run;

proc rank data=heart out=temp groups=15 descending;
by sex;
var weight;
ranks rank;
run;

proc freq data=temp;
table sex*rank/list;
run;
ballardw
Super User

@luciacossaro wrote:

It don't work, I believe the proble is the number of group is biger than the number of observations in some Cle_unique's value.

If you try this code, it don't work. That is the problem with my dataset. I need to class observations to can choose 10% higher in each value of Cle_unique but sometimes this variable have 60 observation and sometime only 4.

 

Thank you for your advice.

 


You need to tell us exactly what does not work. You obviously expect a specific result. Please show what that explicit result would have been if that example code worked correctly.

Ksharp
Super User

OK. I understand what you mean now .

 

%let percent=0.1;

data class;
 set sashelp.class;
run;

proc summary data=class nway;
class sex;
var weight;
output out=n(drop=_:) n=freq;
run;
data n;
 set n;
 n=ceil(&percent*freq);
 drop freq;
run;
proc sort data=class;
by sex descending weight;
run;
data want;
 merge class n;
 by sex;
 if first.sex then count=0;
 count+1;
 if count le n;
 drop count n;
run;

luciacossaro
Obsidian | Level 7

Thank you very much!! this is what i wanted; it's works.

 

 

 

ballardw
Super User

proc sort data=have;

   by vble_a descending Vle_b;

run;

 

Should work if I understand your need and Vle_b is numeric. If Vle_b is not numeric but you want the contents to sort as if they are then you would add sortseq=linguistic (numeric_collation=on) to the proc statement.

 

if that does not work then you need a better description of your sort rule(s) and data examples.

luciacossaro
Obsidian | Level 7

Hi; 

 

I have a dataset with a variable "ID" (char) and a variable "Price" (num) and some others variables.

I need split the dataset in 10 deciles (10% of observations in each) FOR EACH ID'S VALUE depending on price's values for after to can choose the highest 10% price's value for each ID'S value.

 

For the differents ID's values there is not the same number of observations. 

 

I tried with PROC RANK but i get deciles for all the dataset (not depending of each ID's value).

 

It should be relatively easy to do it but I am a new SAS user and I can not do it.

 

Do you have any idea ?

 

Thank you!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 13 replies
  • 1347 views
  • 5 likes
  • 6 in conversation