Help using Base SAS procedures

Need help with proc freq

Reply
New Contributor
Posts: 2

Need help with proc freq

Greetings (think I posted this in the wrong forum earlier),

Currently I have a stored process that does a proc freq on a table of data. The 2 columns of interest are Facility and Contacts. I have a filter setup in the stored process to filter on the Facility. The table looks like this:

Facility, Contacts
A, 1
A, 2
B, 3
B, 10
C, 15


Now... in my stored process has the following code:
proc sort data=opmetric.sas_geo_2010all out=barron;
by descending facilityname descending contacts;
where state in (%stringGen(parm=state)) and facilityname in (%stringGen(parm=facility)); run;

proc freq data=barron order=data noprint;
tables state*city*facilityname / list out=contacts;
weight contacts;
run;
data finalcontacts (drop=percent);
set contacts;
cumcontacts+percent;
run;
data sasbase.geo2010all;
merge barron (in=c) finalcontacts (in=a) finalrounds (in=b);
if c;
run;


Now when I run this, this will calculate the frequency and cumulative % for each facility that I supply in the filter. So if I supplied Facilities A and B, the ending table would look like:
Facility, Contacts, % of Total, Cum %
A, 1, 6.25%, 6.25%
A, 2, 12.5%, 18.75%
B, 3, 18.75%, 37.50%
B, 10, 62.50%, 100%

However, what I would like for it to do is break out a cumulative % per Facility, instead of lumping every facility from the filter together. So Facility A would have its own cumulative %, then Facility B would have its own cumulative %. etc

Hope this makes sense

Thanks!
Valued Guide
Posts: 632

Re: Need help with proc freq

First a couple of things that I noticed in the code:
1) The data set finalrounds mentioned in the MERGE statement is not shown.
2) There is no BY statement associated with the MERGE. This is called a one-to-one merge and is an exceptionally good way to get wrong results. In your case the incoming data (BARRON) are sorted, but the FREQ will not necessarily (in your case almost certainly not) maintain that order in the out= data set (CONTACTS).

To calculate the FACILITY totals use FIRST. and LAST. processing. There are a number of posts on the topic. Your code will include:
[pre]
by ......facility;
. . .
* initialize the facility;
if first.facility then do;
...
* create totals much as is currently being done;
...
* write the facility total.;
if last.facility then do;
[/pre]
Super User
Posts: 10,461

Re: Need help with proc freq

I'm not sure I understand completely what you are attempting but it may be that PROC TABULATE with the ALL predicate, which gives summaries of items in a class, may be a place to look.
Super User
Posts: 9,662

Re: Need help with proc freq

Hi.
I am not sure.Maybe you need add 'by' statement into proc freq;
[pre]
ods html file='class.html' style=sasweb;
proc sort data=sashelp.class out=class;
by sex;
run;
proc freq data=class;
by sex;
tables sex*name /list;
run;
ods html close;
[/pre]


Ksharp
Ask a Question
Discussion stats
  • 3 replies
  • 108 views
  • 0 likes
  • 4 in conversation