VOTE PROGRAM DATASET ANALYSIS PROC PRINT | PROC MEANS | PROC FREQ | PROC SORT | PROC SUMMARY | PROC SQL
*/Creating A Dataset Of VOTE PROGRAM /*
Step 1: Create a dataset for a vote program
options nocenter;
data vote_program;
length VoterID $5 Gender $1 State $15 Party $10 VoteStatus $3;
input VoterID $ Age Gender $ State $ Party $ VoteStatus $;
datalines;
V001 25 M Telangana BJP Yes
V002 42 F Andhra Congress No
V003 34 M Telangana Congress Yes
V004 29 F Karnataka BJP Yes
V005 51 M Telangana BJP No
V006 38 F Andhra BJP Yes
V007 46 M Karnataka Congress No
V008 33 F Telangana Congress Yes
V009 27 M Andhra BJP Yes
;
run;
proc print;run;
Output:
| Obs | VoterID | Gender | State | Party | VoteStatus | Age |
|---|---|---|---|---|---|---|
| 1 | V001 | M | Telangana | BJP | Yes | 25 |
| 2 | V002 | F | Andhra | Congress | No | 42 |
| 3 | V003 | M | Telangana | Congress | Yes | 34 |
| 4 | V004 | F | Karnataka | BJP | Yes | 29 |
| 5 | V005 | M | Telangana | BJP | No | 51 |
| 6 | V006 | F | Andhra | BJP | Yes | 38 |
| 7 | V007 | M | Karnataka | Congress | No | 46 |
| 8 | V008 | F | Telangana | Congress | Yes | 33 |
| 9 | V009 | M | Andhra | BJP | Yes | 27 |
Step 2: View the dataset
proc print data=vote_program(obs=7);
title "Vote Program Dataset";
run;
Output:
| Obs | VoterID | Gender | State | Party | VoteStatus | Age |
|---|---|---|---|---|---|---|
| 1 | V001 | M | Telangana | BJP | Yes | 25 |
| 2 | V002 | F | Andhra | Congress | No | 42 |
| 3 | V003 | M | Telangana | Congress | Yes | 34 |
| 4 | V004 | F | Karnataka | BJP | Yes | 29 |
| 5 | V005 | M | Telangana | BJP | No | 51 |
| 6 | V006 | F | Andhra | BJP | Yes | 38 |
| 7 | V007 | M | Karnataka | Congress | No | 46 |
Step 3: Summary statistics for Age
proc means data=vote_program mean median min max;
var Age;
title "Age Summary Statistics";
run;
Output:
The MEANS Procedure
| Analysis Variable : Age | |||
|---|---|---|---|
| Mean | Median | Minimum | Maximum |
| 36.1111111 | 34.0000000 | 25.0000000 | 51.0000000 |
Step 4: Frequency counts for categorical variables
proc freq data=vote_program;
tables Gender State Party VoteStatus / nocum;
title "Frequency Distribution of Categorical Variables";
run;
Output:
The FREQ Procedure
| Gender | Frequency | Percent |
|---|---|---|
| F | 4 | 44.44 |
| M | 5 | 55.56 |
| State | Frequency | Percent |
|---|---|---|
| Andhra | 3 | 33.33 |
| Karnataka | 2 | 22.22 |
| Telangana | 4 | 44.44 |
| Party | Frequency | Percent |
|---|---|---|
| BJP | 5 | 55.56 |
| Congress | 4 | 44.44 |
| VoteStatus | Frequency | Percent |
|---|---|---|
| No | 3 | 33.33 |
| Yes | 6 | 66.67 |
Step 5: Macro to filter voters by Party and State
%macro filter_voters(party=, state=);
proc print data=vote_program;
where Party="&party" and State="&state";
title "Voters from &state belonging to &party";
run;
%mend filter_voters;
Step 6: Use the macro
%filter_voters(party=BJP, state=Telangana);
Output:
| Obs | VoterID | Gender | State | Party | VoteStatus | Age |
|---|---|---|---|---|---|---|
| 1 | V001 | M | Telangana | BJP | Yes | 25 |
| 5 | V005 | M | Telangana | BJP | No | 51 |
%filter_voters(party=Congress, state=Andhra);
Output:
| Obs | VoterID | Gender | State | Party | VoteStatus | Age |
|---|---|---|---|---|---|---|
| 2 | V002 | F | Andhra | Congress | No | 42 |
Step 7: Sort dataset by State and Age
proc sort data=vote_program out=vote_sorted;
by State Age;
run;
proc print data=vote_sorted;
title "Vote Program Dataset Sorted by State and Age";
run;
Output:
| Obs | VoterID | Gender | State | Party | VoteStatus | Age |
|---|---|---|---|---|---|---|
| 1 | V009 | M | Andhra | BJP | Yes | 27 |
| 2 | V006 | F | Andhra | BJP | Yes | 38 |
| 3 | V002 | F | Andhra | Congress | No | 42 |
| 4 | V004 | F | Karnataka | BJP | Yes | 29 |
| 5 | V007 | M | Karnataka | Congress | No | 46 |
| 6 | V001 | M | Telangana | BJP | Yes | 25 |
| 7 | V008 | F | Telangana | Congress | Yes | 33 |
| 8 | V003 | M | Telangana | Congress | Yes | 34 |
| 9 | V005 | M | Telangana | BJP | No | 51 |
Step 8: Macro to calculate vote percentage by Party
%macro vote_percentage(party=);
proc sql;
select
sum(case when VoteStatus='Yes' then 1 else 0 end) as YesVotes,
count(*) as TotalVotes,
calculated YesVotes / calculated TotalVotes * 100 as VotePercent format=6.2
from vote_program
where Party="&party";
quit;
%mend vote_percentage;
%vote_percentage(party=BJP);
Output:
| YesVotes | TotalVotes | VotePercent |
|---|---|---|
| 4 | 5 | 80.00 |
%vote_percentage(party=Congress);
Output:
| YesVotes | TotalVotes | VotePercent |
|---|---|---|
| 2 | 4 | 50.00 |
No comments:
Post a Comment