BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SU22
Calcite | Level 5

Hi,

I have a dataset as below:

Personnel number.      Code 1         Code 2         Code 3

1.                                   26

2                                     12                  26

3                                     15                                       12

4                                      26

5                                                           15

 

I want to write a code to return the # of times 26 or other codes appear.

Thanks for

1 ACCEPTED SOLUTION
3 REPLIES 3
SASKiwi
PROC Star

PROC FREQ is a good way of doing this:

proc freq data = have;
  table code1 * code2 * code3 / list missing;
run;
andreas_lds
Jade | Level 19

After transposing you could also use proc summary:

proc summary data=long nway;
   class Col1;
   output out=work.counted(drop= _type_ rename=(_freq_=Count));
run;

Or, if you love to write unnecessary long programs, use a hash-object:

data _null_;
   set have end=jobDone;
   
   length Code Count 8;
   
   array codes[3] Code1-Code3;
   
   if _n_ = 1 then do;
      declare hash h(ordered: 'yes');
      h.defineKey('Code');
      h.defineData('Code', 'Count');
      h.defineDone();
   end;
   
   do i = 1 to dim(codes);
      if not missing(codes[i]) then do;
         Code = codes[i];
         
         if h.find() = 0 then do;
            Count = Count + 1;
            h.replace();
         end;
         else do;
            Count = 1;
            h.add();
         end;
      end;
   end;
   
   if jobDone then do;
      h.output(dataset: 'work.hashCount');
   end;
run;

 

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1153 views
  • 1 like
  • 4 in conversation