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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

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;
byeh2017
Quartz | Level 8

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

andreas_lds
Jade | Level 19

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;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 2546 views
  • 0 likes
  • 3 in conversation