DATA Step, Macro, Functions and more

Grouping data in order to count frequencies

Reply
N/A
Posts: 0

Grouping data in order to count frequencies

Hello,

I am relatively new to using SAS beyond the routine PROC proceedures.

I am trying to create a simple frequency distribution chart, but on grouped data.
In other words, I want to define 10 groups, say 0-10, 10-20, ..... 90-100
and then count how many of the originial observations are in each of these 10 groups.
Is there an easy way to do this using PROC UNIVARIATE or such?
Trusted Advisor
Posts: 2,116

Re: Grouping data in order to count frequencies

Posted in reply to deleted_user
The easiest way is to create a FORMAT (see PROC FORMAT for syntax) and then use PROC FREQ to get your frequencies.

Doc Muhlbaier
Duke
Super Contributor
Posts: 474

Re: Grouping data in order to count frequencies

Posted in reply to deleted_user
Doc's suggestion (PROC FORMAT+PROC FREQ) is just right.

More solutions are available, but all very similar, and all within a two pass execution.

Another one, would be two SQL queries or one SQL query with sub-query, (which is still a two pass solution), with the inner query producing some temporary GROUP_ID for each row (say GROUP_ID=1 case when 0-10, GROUP_ID=2 case when 10-20, ...), and the outer query counting row for each GROUP_ID.

Cheers from Portugal.

Daniel Santos @ www.cgd.pt.
N/A
Posts: 0

Re: Grouping data in order to count frequencies

Posted in reply to DanielSantos
I am surprised SAS has not incoporated an easier way to do this.

...... are you all enjoying you legal drugs in Protugal.... just kidding. Wouldn't make a difference to me, but it makes sense economically.
N/A
Posts: 0

Re: Grouping data in order to count frequencies

Posted in reply to DanielSantos
.... does anybody know of an explicit (simple) example where this is done. I am not familiar with the SQL syntax or even the FORMAT syntax.

....... it's no big deal, but I would like to learn it in case I come across it again.
Super Contributor
Posts: 474

Re: Grouping data in order to count frequencies

Posted in reply to deleted_user
But, it is indeed an easy procedure.

For example:

proc sql;
select GROUP_ID, count(*) as COUNT from (
select int(VALUE/10) as GROUP_ID from INPUT
) group by GROUP_ID;
quit;

int(VALUE/10) will return the integer value of VALUE/10, so
0 =< VALUE < 10 => GROUP_ID = 0
10 =< VALUE < 20 => GROUP_ID = 1
etc...

And by the way, drugs are still pretty illegal here.
It was discussed some time ago, but it was a no go idea.

Cheers from Portugal.

Daniel Santos @ www.cgd.pt.
N/A
Posts: 0

Re: Grouping data in order to count frequencies

Posted in reply to DanielSantos
Wow, that's awesome.

I thought I heard on NPR that they decriminalized all drugs in Protugal???
Ask a Question
Discussion stats
  • 6 replies
  • 164 views
  • 0 likes
  • 3 in conversation