Help using Base SAS procedures

Comparing same variable (freq, count, etc) from two datasets

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

Comparing same variable (freq, count, etc) from two datasets

[ Edited ]

Hi experts!

 

So, I have two datasets, a midline and an endline. They have identical variables (different observations though, obviously) and I want to be able to compare changes in frequencies, etc between the two without having to go line by line in my output tables from each respective data sets' proc freq. I have 4 districts I'm looking at, and all of my proc freqs are stratified by these districtss. So, for example, what if I want to know the differences between how many said x, y, or z in response to indicator 4, from each set? Is there a way to do this? 

 

I found this macro: http://www2.sas.com/proceedings/sugi26/p097-26.pdf but to be honest I have no idea how to input the info related to my data sets. I also tried proc compare, but I have no idea what the output means. As I'm sure you can tell, I'm pretty limited in my SAS knowledge and have been having trouble with the more nuanced procedures/code.

 

ANY help at all would be greatly appreciated! Thank you!


Accepted Solutions
Solution
‎02-25-2016 03:34 PM
Community Manager
Posts: 2,757

Re: Comparing same variable (freq, count, etc) from two datasets

[ Edited ]

Almost there.  Just need to sort by the dist variable:

 

Data stacked;
Set data1 data2 indsname=source;
Dset=source;
Run;

proc sort data=stacked; by dist; run;

Proc freq data=stacked;
By dist;
Table tb*dset;
Run;


The INDSNAME option takes the name of the origin data set for each record and puts it into a category variable (DSET, eventually) so you can compare the two populations.

View solution in original post


All Replies
Super User
Posts: 17,780

Re: Comparing same variable (freq, count, etc) from two datasets

Make one dataset and then run your proc freqs with the source as a crosstab variable. 

 

Data stacked;
Set data1 data2 indsname=source;
Dset=source;
Run;

Proc freq data=stacked;
By district;
Table varx * dset;
Run;

 

Contributor
Posts: 22

Re: Comparing same variable (freq, count, etc) from two datasets

[ Edited ]

I'm afraid I got this error:

Proc freq data=stacked;
1425 By dist;
1426 Table tb*dset;
1427 Run;

NOTE: Writing HTML Body file: sashtml.htm
ERROR: Data set WORK.STACKED is not sorted in ascending sequence. The current BY group has
dist = Samfya and the next BY group has dist = Luwingu.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 343 observations read from the data set WORK.STACKED.
NOTE: PROCEDURE FREQ used (Total process time):
real time 1.29 seconds
cpu time 0.32 seconds

 

And this output:

The FREQ Procedure

 

 

Frequency Percent Row Pct Col Pct
Table of tb by Dset tb Dset data1 Total 1 2 Total
124
72.94
100.00
72.94
124
72.94
 
 
46
27.06
100.00
27.06
46
27.06
 
 
170
100.00
170
100.00

 

 

Which is obviously only half of what I want. I apologize for being so new, I just don't know what this code is doing exactly. I understand combining the two datasets, but what exactly is the indsname option, or "source?" 

 

Data stacked;
Set data1 data2 indsname=source;
Dset=source;
Run;

Proc freq data=stacked;
By district;
Table tb*dset;
Run;

That is what I ran... 

 

Solution
‎02-25-2016 03:34 PM
Community Manager
Posts: 2,757

Re: Comparing same variable (freq, count, etc) from two datasets

[ Edited ]

Almost there.  Just need to sort by the dist variable:

 

Data stacked;
Set data1 data2 indsname=source;
Dset=source;
Run;

proc sort data=stacked; by dist; run;

Proc freq data=stacked;
By dist;
Table tb*dset;
Run;


The INDSNAME option takes the name of the origin data set for each record and puts it into a category variable (DSET, eventually) so you can compare the two populations.

Contributor
Posts: 22

Re: Comparing same variable (freq, count, etc) from two datasets

Thank you for clarifying! And @Reeza, thank you for that first step!

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 269 views
  • 1 like
  • 3 in conversation