Please I need assistance with Proc sort.
I sorted a data set by two different variables; SALES and PRODUCT. I want the product variable to be sorted in descending order while the sales variable is sorted in ascending order.
Here is my program:
Proc sort data = sashelp.shoes out = work.sortedshoes;
by sales descending product;
run;
The SALES variable gets sorted in ascending order but the PRODUCT variable was never sorted in descending order.
I also re-instructed as below:
Proc sort data = sashelp.shoes out = work.sortedshoes;
by descending product;
Proc sort data = work.shoes out = work.sortedshoes2;
by sales;
run;
....but the PRODUCT variable never still gets sorted in descending order. Please does anyone know what could be the issue?
Thanks!
Show example data. You cannot sort a single dataset in two different ways.
Your first PROC SORT does what you asked for. Just look at the resulting dataset.
proc sort data = sashelp.shoes out = work.sortedshoes;
by sales descending product;
run;
proc print data=sortedshoes (obs=10);
var sales product;
run;
As you can see the values of SALES are increasing.
Obs Sales Product 1 $325 Sandal 2 $449 Sport Shoe 3 $450 Sport Shoe 4 $554 Sandal 5 $601 Sandal 6 $712 Sandal 7 $736 Sandal 8 $737 Sandal 9 $801 Sport Shoe 10 $936 Sport Shoe
To tell if the values of PRODUCT are decreasing within a given value of SALES just look at the observations that have the same value of SALES.
data dups;
set sortedshoes;
by sales;
if not (first.sales and last.sales);
run;
proc print;
var sales product;
run;
Result:
Obs Sales Product 1 $1,190 Sandal 2 $1,190 Sandal 3 $1,520 Sandal 4 $1,520 Boot 5 $8,365 Sport Shoe 6 $8,365 Boot
So you can see for the observations were SALES are 1,520 or 8,365 the two different values or PRODUCT are sorted in DESCENDING order just like you asked for.
Hardly possible to help without seeing the data used. Also please post the log, as text, so that we see what happened actually.
The descending sort of product would only be visible within groups that share the same sales value. Since this is almost never the case:
69 proc sort data=sashelp.shoes out=check nodupkey; 70 by sales; 71 run; NOTE: There were 395 observations read from the data set SASHELP.SHOES. NOTE: 3 observations with duplicate key values were deleted. NOTE: The data set WORK.CHECK has 392 observations and 7 variables.
the product values seem to not be sorted.
are you sure it's incorrect? show us a proc print of work.sortedshoes
Show example data. You cannot sort a single dataset in two different ways.
Your first PROC SORT does what you asked for. Just look at the resulting dataset.
proc sort data = sashelp.shoes out = work.sortedshoes;
by sales descending product;
run;
proc print data=sortedshoes (obs=10);
var sales product;
run;
As you can see the values of SALES are increasing.
Obs Sales Product 1 $325 Sandal 2 $449 Sport Shoe 3 $450 Sport Shoe 4 $554 Sandal 5 $601 Sandal 6 $712 Sandal 7 $736 Sandal 8 $737 Sandal 9 $801 Sport Shoe 10 $936 Sport Shoe
To tell if the values of PRODUCT are decreasing within a given value of SALES just look at the observations that have the same value of SALES.
data dups;
set sortedshoes;
by sales;
if not (first.sales and last.sales);
run;
proc print;
var sales product;
run;
Result:
Obs Sales Product 1 $1,190 Sandal 2 $1,190 Sandal 3 $1,520 Sandal 4 $1,520 Boot 5 $8,365 Sport Shoe 6 $8,365 Boot
So you can see for the observations were SALES are 1,520 or 8,365 the two different values or PRODUCT are sorted in DESCENDING order just like you asked for.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.