Programming the statistical procedures from SAS

Merging Proc Freq with a file

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Merging Proc Freq with a file

I ran a PROC FREQ by state. The original file contained several rows for state, and the PROC FREQ summed the states into one line. How do I take the PROC FREQ and merge it with an actual file?

Thank you for the help!

Nicole


Accepted Solutions
Solution
‎12-12-2011 09:52 PM
Super User
Super User
Posts: 6,711

Merging Proc Freq with a file

But which of the 67 rows do you want? 

Perhaps you want to summarize the 67 rows?

You might consider PROC SUMMARY (also known as PROC MEANS).

For example to get the mean of all the numeric variables (like the two mentioned above in your example code).

You can use a CLASS statement even if the data is not sorted by that variable.

proc summary data=walmart7 nway ;

   class state ;

   var _numeric_ ;

   output out=means mean= ;

run;

View solution in original post


All Replies
PROC Star
Posts: 7,417

Merging Proc Freq with a file

It will be easier to answer your question if you provide some sample data and your proc freq code.

Occasional Contributor
Posts: 5

Merging Proc Freq with a file

data walmart7;

set walmartfin1;

where newin1990 = 1 ;

if dist gt 400 and dist lt 800 then output;

run;

proc sort data = walmart7;

by state;

run;

proc freq data = walmart7;

output;

run;

PROC Star
Posts: 7,417

Merging Proc Freq with a file

????????????

That proc freq won't produce anything.  And, by data, I meant a datastep that produces a small subset of what your data really look like.

Super User
Super User
Posts: 6,711

Merging Proc Freq with a file

What about the PROC FREQ output do you want to merge back onto the original data?

Do you want the count of the number of records per state?

proc freq data=walmart7 ;

  tables state / out=counts ;

run;

data want ;

  merge counts walmart7 ;

  by state ;

run;

Occasional Contributor
Posts: 5

Merging Proc Freq with a file

Tom,

This is very helpful, thank you for your help! I have one additional question if you have the time. In the table, the state of Alabama shows up in 67 rows. Do you know how I would go about getting one row per state?

Thanks again for your help,

Nicole

Solution
‎12-12-2011 09:52 PM
Super User
Super User
Posts: 6,711

Merging Proc Freq with a file

But which of the 67 rows do you want? 

Perhaps you want to summarize the 67 rows?

You might consider PROC SUMMARY (also known as PROC MEANS).

For example to get the mean of all the numeric variables (like the two mentioned above in your example code).

You can use a CLASS statement even if the data is not sorted by that variable.

proc summary data=walmart7 nway ;

   class state ;

   var _numeric_ ;

   output out=means mean= ;

run;

Super User
Posts: 18,601

Merging Proc Freq with a file

Using SQL to get the counts in is a bit easier.

Replace have with your table name and it should work.

Proc SQL;

create table want as

select *, count(state) as num_obs

from have

group by state;

quit;

🔒 This topic is solved and locked.

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

Discussion stats
  • 7 replies
  • 156 views
  • 9 likes
  • 4 in conversation