Help using Base SAS procedures

Grouping an data

Reply
Frequent Contributor
Posts: 90

Grouping an data

Hi ,

I need a help to get an deisred output as below,

(Need to consider PS1 ,PS2  as PS)

CODE                      SPECIALTY

PS                                OBGYN

PS                                 SURG

PS1                               OBGYN

PS1                                 ANES

PS1                                  NEPH

PS2                                OBGYN

PS2                                  NEPH

OUTPUT SHOULD BE APPEAR AS BELOW,

CODE                       SPECIALTY           OCCURRENCES

PS                               OBGYN                            3

PS                                 SURG                            1

PS                                  ANES                            1

PS                                   NEPH                           2

Thanks in Advance..

SAS Super FREQ
Posts: 8,739

Grouping an data

Hi:

   What code have you tried? How do you want to see the results? As a SAS dataset? As output in the SAS LISTING window? As an HTML output file? As RTF or PDF output?  As a CSV file?

  There are several different SAS procedures that will produce this type of report. You might need a bit of data manipulation or special user-defined format to take care of the recoding for the CODE variable. The procedure and method you use really depends on what your desired end result is.

cynthia

Frequent Contributor
Posts: 90

Grouping an data

Hi Cynthia,

I want to see the results as a SAS dataset.

Thanks

SAS Super FREQ
Posts: 8,739

Grouping an data

Hi:

  You did not say what code you had tried.

  I can think of PROC REPORT, PROC FREQ, PROC TABULATE, PROC SQL or PROC MEANS or a custom DATA step program, as possible ways to create an output SAS dataset from the data you posted. Most of the procedure methods support an OUT= option to create an output dataset. The DATA step approach would use FIRST.and LAST. BY processing to accumulate an OCCURENCE variable for CODE/SPECIALITY combinations. Many different possibilities...

cynthia

Frequent Contributor
Posts: 90

Grouping an data

Cynthia,

I tried this below code,

data d;
  set specinfoo ;
  by pancod spccod;
  if first.spccod then occurrences=0;
  occurrences+1;
  if last.spccod then output;
run;

proc sort data=d out=e;
  by pancod occurrences;
run;

data f;
  set e;
  by pancod;
  if last.pancod then output;
run;

PROC Star
Posts: 7,356

Grouping an data

How about trying something like:

data need;

  set have (rename=(code=code_in));

  code=compress(code_in,,'ka');

run;

proc sort data=need;

  by code;

run;

ods listing close;                                                               

ods output OneWayFreqs=want;                                                  

                                                                                 

proc freq data=need (keep=code specialty);                                                    

   tables specialty;

   by code;

run;                                                                             

                                                                                 

ods output close;                                                                

ods listing;                                                                     

Regular Contributor
Posts: 184

Grouping an data

PROC FREQ is the SAS component designed to count things. It looks like this problem can be solved in one step, using built-in features.

data have ;

input

CODE $                    SPECIALTY $      ; cards ;   

PS                                OBGYN

PS                                 SURG

PS1                               OBGYN

PS1                                 ANES

PS1                                  NEPH

PS2                                OBGYN

PS2                                  NEPH

;

proc freq data=have noprint ;

by code ;

format code $2. ;

tables specialty /

out=want( rename = (count=OCCURRENCES) drop = percent ) ;

run ;

raveena wrote:

Hi ,

I need a help to get an deisred output as below,

(Need to consider PS1 ,PS2  as PS)

CODE                      SPECIALTY

PS                                OBGYN

PS                                 SURG

PS1                               OBGYN

PS1                                 ANES

PS1                                  NEPH

PS2                                OBGYN

PS2                                  NEPH

OUTPUT SHOULD BE APPEAR AS BELOW,

CODE                       SPECIALTY           OCCURRENCES

PS                               OBGYN                            3

PS                                 SURG                            1

PS                                  ANES                            1

PS                                   NEPH                           2

Thanks in Advance..

Contributor
Posts: 23

Grouping an data

Hi,

A simple modification to Howles' code should give you the exact ordered output you want.

Just add the order = data option to the proc freq statement and you will get the order you

want for specialty instead of alphabetical order.

Rich

Super User
Posts: 9,662

Grouping an data

It is another varying version of  Howles's code .

data have ;
input CODE $   SPECIALTY $  ; 
cards ;   
PS  OBGYN
PS  SURG
PS1 OBGYN
PS1 ANES
PS1 NEPH
PS2 OBGYN
PS2 NEPH
;
run;
proc freq data=have noprint;
 format code $2.;
 tables code*specialty/list out=want(drop=percent) nocum nopercent;
run;

Ksharp

Ask a Question
Discussion stats
  • 8 replies
  • 160 views
  • 0 likes
  • 6 in conversation