BookmarkSubscribeRSS Feed
urban58
Quartz | Level 8

I ran the following code as I want a table that shows timeperiod in descending order, and techid and female in ascending order

 

proc means data =have n mean std  min max maxdec=2;

class timeperiod techid female / descending;

var var1 var2 ;

types techid timeperiod*techid*female ;

run;

 

This is what I got – I also tried sorting as below before the proc means but it made no difference

proc sort; by timeperiod descending techid female; run;

 

Can anyone help!

timeperiod

techid

female

2020_P3

862

1

0

825

1

0

2020_P1

825

1

0

845

1

0

2019_P3

825

1

0

845

1

0

5 REPLIES 5
FreelanceReinh
Jade | Level 19

Hi @urban58,

 

Use two CLASS statements:

class timeperiod / descending;
class techid female;

 

Edit: Here's a fully worked example (restricted to one statistic and one analysis variable for simplification):

data have;
do timeperiod='2019_P3', '2020_P1', '2020_P3';
  do techid=845, 825, 862;
    do female=0, 1;
      do _n_=1 to 10;
        var1=rand('uniform');
        output;
      end;
    end;
  end;
end;
run;

proc means data=have n;
class timeperiod / descending;
class techid female;
var var1;
types techid timeperiod*techid*female;
run;

Output:

 Analysis Variable : var1

                  N
      techid    Obs      N
--------------------------
         825     60     60

         845     60     60

         862     60     60
--------------------------


               Analysis Variable : var1

                                                N
timeperiod          techid          female    Obs      N
--------------------------------------------------------
2020_P3                825               0     10     10

                                         1     10     10

                       845               0     10     10

                                         1     10     10

                       862               0     10     10

                                         1     10     10

2020_P1                825               0     10     10

                                         1     10     10

                       845               0     10     10

                                         1     10     10

                       862               0     10     10

                                         1     10     10

2019_P3                825               0     10     10

                                         1     10     10

                       845               0     10     10

                                         1     10     10

                       862               0     10     10

                                         1     10     10
--------------------------------------------------------
As desired, the output shows timeperiod in descending order, and techid (in both tables) and female in ascending order -- regardless of the sort order in dataset HAVE (see the DO statements).

 

urban58
Quartz | Level 8

sorry for the delay in responding.

Both yours and ballardw code/advice gave the same output for my data - the output was great except techid did not sort properly except for the latest timeperiod.

There is something with my data which I'm trying to understand.

Much thanks,

Margaret

FreelanceReinh
Jade | Level 19

@urban58 wrote:

(...) techid did not sort properly except for the latest timeperiod.

There is something with my data which I'm trying to understand.

One common reason for strange, unexpected sort orders are blanks or other invisible characters in character variables. Is techid a character variable (according to PROC CONTENTS output)? And, if so, what is the defined length of the variable (again: see PROC CONTENTS output)? Does techid have hundreds of different values or would it be feasible to review a PROC FREQ output showing the distinct values?

 

What does the improper sort order look like, compared to the proper sort order in the latest timeperiod? Can you show us the relevant portion of your PROC MEANS output or even a sample of your data so that we can reproduce the issue? Obviously, the analysis variables (var1, var2) are irrelevant and can be omitted.

ballardw
Super User

When you sorted the data did you use /order = data on the class statement?

With proc sort you place Descending BEFORE the variable that you want sorted in descending order.

So you want

by descending timeperiod techid female;

 

You may also want to change the order of the Types list. TYPES interacts with the order of the CLASS variables.

 

Without actual data we can't test code.

Reeza
Super User
Do you mean descending based on the values of the time_period or of the values that are summarized?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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