DATA Step, Macro, Functions and more

Count frequency in the columns?

Accepted Solution Solved
Reply
Super Contributor
Posts: 398
Accepted Solution

Count frequency in the columns?

[ Edited ]

Hello:

 

I would like to count the group frequency of the two columns "Analysis" and "Product" in test1 table below.  Please advice how to approach.  Thanks.

 

data test1; 
      infile datalines dlm='|'; 
  input name : $20. Analysis : $100. Product : $100.; 
datalines;                  
Ali| Cleft Lip +/- Palate|  LABETOLOL|   
Ali| Cleft Lip +/- Palate|  ATENOLOL|   
Ali| VSDs (Excludes VSDavc)|  ATENOLOL|  
Ali| VSDs (Excludes VSDavc)|  FLUCONAZOLE| 
Carol| Choanal Atresia|  LEVOTHYROXINE SODIUM|   
Carol| Cleft Lip +/- Palate|  ATENOLOL|   
Carol| Intestinal Atresia/Stenosis|  EPHEDRA|  
Malyn| TGA +/- DORV|  ALBUTEROL SULFATE|  
Malyn|  TOF +/- PA, DORV -TGA|  BUTALBITAL| 
Malyn| Esoph Atresia +/- TEF|  FLONASE|   
; 

proc sort data=test1; by analysis; run;

 

 

 

The result I am looking for is:

Analysis                                            Count

Cleft Lip +/- Palate                            3

VSDs (Excludes VSDavc)                 2

Choanal Atresia                                1

Intestinal Atresia/Stenosis                1

TGA +/- DORV                                 1

TOF +/- PA, DORV -TGA                 1

Esoph Atresia +/- TEF                     1

 

Product

LABETOLOL                                   1

ATENOLOL                                     2

FLUCONAZOLE                             1

LEVOTHYROXINE SODIUM          1

EPHEDRA                                       1

ALBUTEROL SULFATE                  1

BUTALBITAL                                   1

FLONASE


Accepted Solutions
Solution
‎02-07-2018 10:47 AM
Super User
Posts: 13,941

Re: Count frequency in the columns?

Please post example code  in a code box opened with either the {I} or running man icon.

Please.

Do you really want a record of data with no values (the blank dataline)?

 

Is the ORDER of the appearance important? If so you might specify the rules.

If I wanted the output in descending order of frequency I would start with

proc freq data=test1 order=freq;
   tables analysis product /nocum nopercent;
run;

View solution in original post


All Replies
Solution
‎02-07-2018 10:47 AM
Super User
Posts: 13,941

Re: Count frequency in the columns?

Please post example code  in a code box opened with either the {I} or running man icon.

Please.

Do you really want a record of data with no values (the blank dataline)?

 

Is the ORDER of the appearance important? If so you might specify the rules.

If I wanted the output in descending order of frequency I would start with

proc freq data=test1 order=freq;
   tables analysis product /nocum nopercent;
run;
Super Contributor
Posts: 398

Re: Count frequency in the columns?

I don't know how to post example code in a code box opened with either the {I} or running man icon.   I add { } before and after my codes.  Does it help?

 

How to generate a permeant SAS dataset instead of in the "result viewer"?   Is there a way to output in one table?  Thanks.

Super User
Posts: 24,010

Re: Count frequency in the columns?

You should learn how. It makes it easier on us to help you.

 

delete_insert_code.JPG

 

And please try reading the documentation BEFORE asking questions. 

The first example in PROC FREQ:

Example 3.1 Output Data Set of Frequencies

 

http://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.2&docsetId=procstat&docsetTarget=proc...

 


ybz12003 wrote:

I don't know how to post example code in a code box opened with either the {I} or running man icon.   I add { } before and after my codes.  Does it help?

 

How to generate a permeant SAS dataset instead of in the "result viewer"?   Is there a way to output in one table?  Thanks.


 

Super User
Posts: 13,941

Re: Count frequency in the columns?


ybz12003 wrote:

 

How to generate a permeant SAS dataset instead of in the "result viewer"?   Is there a way to output in one table?  Thanks.


Proc freq can create one output data set per tables request which would be for one variable. You could append two data sets to get one though.

 

HOW would you use the single data set with both variables summarized? There can be significant issues in what needs to be done to use such a summarized set depending on exact usage.

Super User
Posts: 24,010

Re: Count frequency in the columns?

You can use ODS to get both into one table, demoed below

 

https://gist.github.com/statgeek/e0903d269d4a71316a4e

Super Contributor
Posts: 398

Re: Count frequency in the columns?

Thanks for all your great suggestion.

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 124 views
  • 3 likes
  • 3 in conversation