coderX
coderX

Reputation: 474

Sort and Print Data by first two letters

I have a data set which contains two variables; name and rank. A rank is attached to each name, and I first must gather the frequency of the most used first two letters (in this example, the two most frequent beginnings to names are Ja and Jo). After finding the two most frequent first two letters (Ja and Jo) I must sort these full names, beginning with the highest rank. The order goes: highest to lowest ranked 'Ja' name, followed by the highest to lowest ranked 'Jo' name. After sorted, I must then produce two separate outputs, one for each of the two 'first two letters'. All 'Ja' names in one output, all 'Jo' names in another. This description may be very confusing, so I apologize. This is the code I have, but I must be doing something wrong. The furthest I can get is sorting the names by either rank or name.

proc freq data = BabyNames order=freq;
        Tables Name/ nocum nopercent;
        Where Name contains 'Ja' or
        Name contains 'Jo';
run;


proc sort data = BabyNames OUT = SortNames;
    By Name descending Rank;
    Where Name contains 'Ja' or
          Name contains 'Jo';
run;

proc print data = SortNames NOOBS;
    By Name;
    Where Name contains 'Ja' or
          Name contains 'Jo';
    Title 'Sorted J Names';
run;

Upvotes: 1

Views: 136

Answers (2)

Ramu
Ramu

Reputation: 165

You are finding Frequencies for names only. How can you sort that data with rank variable?? There is no rank variable in freq output. How can you sort it? Hope you understood.

proc freq data = BabyNames order=freq;
        Tables Name*rank/ nocum nopercent out=need;
        Where Name contains 'Ja' or
        Name contains 'Jo';
run;
proc sort data = need OUT = SortNames;
    By Name descending Rank;
    Where Name contains 'Ja' or
          Name contains 'Jo';
run;

Upvotes: 1

Tom
Tom

Reputation: 51611

You can do it in one step using PROC SQL, because SAS will re-merge the counts back to the detailed records. Here is an example using the SASHELP.CLASS dataset and using AGE as the RANK variable.

proc sql noprint ;
  create table want as
   select substr(name,1,2) as two_letter
        , count(*) as freq
        , name
        , age as rank
   from sashelp.class
   group by 1
   order by 2 desc,4 desc
  ;
quit;
proc print;
run;

Upvotes: 1

Related Questions