Friday, 19 September 2025

291.VOTE PROGRAM DATASET ANALYSIS PROC PRINT | PROC MEANS | PROC FREQ | PROC SORT | PROC SUMMARY | PROC SQL

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:

ObsVoterIDGenderStatePartyVoteStatusAge
1V001MTelanganaBJPYes25
2V002FAndhraCongressNo42
3V003MTelanganaCongressYes34
4V004FKarnatakaBJPYes29
5V005MTelanganaBJPNo51
6V006FAndhraBJPYes38
7V007MKarnatakaCongressNo46
8V008FTelanganaCongressYes33
9V009MAndhraBJPYes27


Step 2: View the dataset

proc print data=vote_program(obs=7);

    title "Vote Program Dataset";

run;

Output:

Vote Program Dataset

ObsVoterIDGenderStatePartyVoteStatusAge
1V001MTelanganaBJPYes25
2V002FAndhraCongressNo42
3V003MTelanganaCongressYes34
4V004FKarnatakaBJPYes29
5V005MTelanganaBJPNo51
6V006FAndhraBJPYes38
7V007MKarnatakaCongressNo46

Step 3: Summary statistics for Age 

proc means data=vote_program mean median min max;

    var Age;

    title "Age Summary Statistics";

run;

Output:

Age Summary Statistics

The MEANS Procedure

Analysis Variable : Age
MeanMedianMinimumMaximum
36.111111134.000000025.000000051.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:

Frequency Distribution of Categorical Variables

The FREQ Procedure

GenderFrequencyPercent
F444.44
M555.56
StateFrequencyPercent
Andhra333.33
Karnataka222.22
Telangana444.44
PartyFrequencyPercent
BJP555.56
Congress444.44
VoteStatusFrequencyPercent
No333.33
Yes666.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:

Voters from Telangana belonging to BJP

ObsVoterIDGenderStatePartyVoteStatusAge
1V001MTelanganaBJPYes25
5V005MTelanganaBJPNo51

%filter_voters(party=Congress, state=Andhra);

Output:

Voters from Andhra belonging to Congress

ObsVoterIDGenderStatePartyVoteStatusAge
2V002FAndhraCongressNo42

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:

Vote Program Dataset Sorted by State and Age

ObsVoterIDGenderStatePartyVoteStatusAge
1V009MAndhraBJPYes27
2V006FAndhraBJPYes38
3V002FAndhraCongressNo42
4V004FKarnatakaBJPYes29
5V007MKarnatakaCongressNo46
6V001MTelanganaBJPYes25
7V008FTelanganaCongressYes33
8V003MTelanganaCongressYes34
9V005MTelanganaBJPNo51

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:

YesVotesTotalVotesVotePercent
4580.00


%vote_percentage(party=Congress);

Output:

YesVotesTotalVotesVotePercent
2450.00



To Visit My Previous Proc  Means And Nway Option:Click Here
To Visit My Previous Proc Means And CharType Option:Click Here
To Visit My Previous SAS Functions:Click Here
To Visit My Previous Length Statement Using In Many Ways:Click Here



Follow Us On : 


 


--- FOLLOW OUR BLOG FOR MORE INFORMATION.

--->PLEASE DO COMMENTS AND SHARE OUR BLOG.

No comments:

Post a Comment