Help using Base SAS procedures

sorting my final table by descending frequency

Accepted Solution Solved
Reply
Super Contributor
Posts: 301
Accepted Solution

sorting my final table by descending frequency

Hi all,

I need to sorting out my final dataset by descending frequency in aesoc and aept, by I do not know how:

I've go the next code:

data have;

length aesoc aept $10.;

input aesoc aept;

datalines;

head factor1

head factor1

head factor2

mouth facto3

mouth factor4

liver factor 5

liver factor6

liver factor6

liver factor 6

;

run;

proc sql noprint;

create table have1 as

select count(aesoc) as nsoc,aesoc

from have

group by aesoc

order by aesoc;

quit;

proc sql noprint;

create table have2 as

select count(aept) as npt,aesoc,aept

from have

group by aesoc,aept

order by aesoc,aept;

quit;

proc sql noprint;

create table have3 as

select a.*, b.npt,b.aept

from have1 as a left join have2 as b

on a.aesoc=b.aesoc;

quit;

data final (drop=aesoc aept);

  set have3;

  by aesoc aept;

  if first.aesoc then do;

  trt=put(nsoc,3.);

  aeterm=upcase(aesoc);

  output;

  end;

  if first.aept then do;

  trt=put(npt,3.);

  aeterm=aept;

  output;

  end;

  run;

  proc print data=final noobs ;

  run;

This is the result I get ***, but as you can see I need to sorting it in terms of descending frequency, somethig like : LIVER first (factor,factor6), HEAD(factor1,factor2) and finally MOUTH (factor3 factor4) 

***** (this is not sorting out by descendig frequency, it has been sorting out by AESOC, AEPT)

                                  nsoc    npt    trt    aeterm

                                    3      2      3     HEAD

                                    3      2      2     factor1

                                    3      1      1     factor2

                                    4      2      4     LIVER

                                    4      2      2     factor

                                    4      2      2     factor6

                                    2      1      2     MOUTH

                                    2      1      1     facto3

                                    2      1      1     factor4

Thanks in advance,

V.


Accepted Solutions
Solution
‎05-03-2013 01:20 PM
Super Contributor
Posts: 301

Re: sorting my final table by descending frequency

...I found the solution...Thanks.

......

proc sort data=have3;by descending nsoc descending npt;run; *sorting by descending frequency;

data final (drop=aesoc aept);

  set have3;

  by aesoc aept notsorted; ****to keep the code intact;

  if first.aesoc then do;

  trt=put(nsoc,3.);

  aeterm=upcase(aesoc);

  output;

  end;

  if first.aept then do;

  trt=put(npt,3.);

  aeterm=aept;

  output;

  end;

  run;

View solution in original post


All Replies
Solution
‎05-03-2013 01:20 PM
Super Contributor
Posts: 301

Re: sorting my final table by descending frequency

...I found the solution...Thanks.

......

proc sort data=have3;by descending nsoc descending npt;run; *sorting by descending frequency;

data final (drop=aesoc aept);

  set have3;

  by aesoc aept notsorted; ****to keep the code intact;

  if first.aesoc then do;

  trt=put(nsoc,3.);

  aeterm=upcase(aesoc);

  output;

  end;

  if first.aept then do;

  trt=put(npt,3.);

  aeterm=aept;

  output;

  end;

  run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 1 reply
  • 299 views
  • 0 likes
  • 1 in conversation