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;

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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