DATA Step, Macro, Functions and more

Proc freq count of multiple variables treated as 1 variable

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 77
Accepted Solution

Proc freq count of multiple variables treated as 1 variable

I have a dataset with 4 variables that basically is the same variable that house intervention codes. Is there a proc freq code that I can use to treat the 4 variables the same variable to get an overall frequency distribution of all 4 variables? Here is the sample dataset.

 

data SAMPINT;
  infile datalines dsd truncover;
  input ID:BEST. Intervention1:$3. Intervention2:$3. Intervention3:$3. Intervention4:$3.;
datalines4;
1,BB8,ABC,ABC,
2,BB8,ABC,CCC,
3,,BB8,,
4,,,BB8,
5,,,,BB8
6,,CCC,,
7,ABC,,,
8,,BB8,,ABC
9,,BB8,,
10,,,,
;;;;

Essentially, I would like the output to look like this:

 

InterventionFrequency
BB87
ABC5
CCC1

 

Thanks alot for your help!


Accepted Solutions
Solution
‎04-25-2017 09:42 AM
PROC Star
Posts: 746

Re: Proc freq count of multiple variables treated as 1 variable

[ Edited ]

Something like this?

 

data SAMPINT;
  infile datalines dsd truncover;
  input ID:BEST. Intervention1:$3. Intervention2:$3. Intervention3:$3. Intervention4:$3.;
datalines4;
1,BB8,ABC,ABC,
2,BB8,ABC,CCC,
3,,BB8,,
4,,,BB8,
5,,,,BB8
6,,CCC,,
7,ABC,,,
8,,BB8,,ABC
9,,BB8,,
10,,,,
;;;;

proc transpose data=SAMPINT
   out=long_SAMPINT(rename=(Col1=Intervention) drop = _NAME_);                    
   by ID;               
   var Intervention1-Intervention4;
run;

proc freq data = long_SAMPINT;
	tables Intervention / nocum nopercent;
run;

View solution in original post


All Replies
Solution
‎04-25-2017 09:42 AM
PROC Star
Posts: 746

Re: Proc freq count of multiple variables treated as 1 variable

[ Edited ]

Something like this?

 

data SAMPINT;
  infile datalines dsd truncover;
  input ID:BEST. Intervention1:$3. Intervention2:$3. Intervention3:$3. Intervention4:$3.;
datalines4;
1,BB8,ABC,ABC,
2,BB8,ABC,CCC,
3,,BB8,,
4,,,BB8,
5,,,,BB8
6,,CCC,,
7,ABC,,,
8,,BB8,,ABC
9,,BB8,,
10,,,,
;;;;

proc transpose data=SAMPINT
   out=long_SAMPINT(rename=(Col1=Intervention) drop = _NAME_);                    
   by ID;               
   var Intervention1-Intervention4;
run;

proc freq data = long_SAMPINT;
	tables Intervention / nocum nopercent;
run;
Frequent Contributor
Posts: 77

Re: Proc freq count of multiple variables treated as 1 variable

Thanks. Is there a way to limit the final output to the top 5 observations in descending order?

Super Contributor
Posts: 345

Re: Proc freq count of multiple variables treated as 1 variable

If you are fond of writing code, using a hash object can replace both procs.

 

data _null_;
   set work.sampint end=done;

   length Intervention $ 3 Frequency 8;

   array inter Intervention1-Intervention4;

   if _n_ = 1 then do;
      declare hash h(/*ordered: 'YES'*/);
      h.defineKey('Intervention');
      h.defineData('Intervention', 'Frequency');
      h.defineDone();
   end;

   do i = 1 to dim(inter);
      if not missing(inter[i]) then do;
         Intervention = inter[i];

         if h.find() ^= 0 then do;            
            Frequency = 1;
            h.add();
         end;
         else do;
            Frequency = Frequency + 1;
            h.replace();
         end;
      end;
   end;

   if done then do;
      h.output(dataset: 'work.want');
   end;
run;

proc print data=work.want;run;
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 226 views
  • 0 likes
  • 3 in conversation