210.ANALYZING DIFFERENT TYPES OF IPS TOPPERS BY BACKGROUND STATE RANK | GENDER | YEAR | SUBJECT | AGE | USING | PROC SQL | PROC MEANS | PROC FREQ | PROC PRINT | PROC SORT | PROC FORMAT | PROC MACRO | DATA STEP IN SAS

ANALYZING DIFFERENT TYPES OF IPS TOPPERS BY BACKGROUND STATE RANK | GENDER | YEAR | SUBJECT | AGE | USING | PROC SQL | PROC MEANS | PROC FREQ | PROC PRINT | PROC SORT | PROC FORMAT | PROC MACRO | DATA STEP IN SAS

/*Creating a simulated SAS dataset of IPS toppers with information on their rank, educational background, state, gender, and optional subjects*/

Step 1: Dataset Creation — IPS_Toppers

data IPS_Toppers;

    length Topper_ID 8 Name $20 Gender $6 State $15 Background $20 

           Optional_Subject $25 Year 8 Rank 8 Age 8;

    input Topper_ID Name $ Gender $ State $ Background $ 

          Optional_Subject $ Year Rank Age;

    datalines;

1 Aarav M Delhi Engineering PSIR 2020 1 26

2 Sneha F Maharashtra Medical Sociology 2020 2 25

3 Raj M TamilNadu Arts Geography 2021 3 27

4 Kavya F Kerala Engineering Anthropology 2021 4 24

5 Aditya M UP Commerce PSIR 2021 5 28

6 Meera F Karnataka Arts History 2022 6 25

7 Rohit M Punjab Law PSIR 2022 7 26

8 Priya F Haryana Engineering Geography 2022 8 24

9 Arjun M Gujarat Science Sociology 2023 9 27

10 Isha F MP Commerce Anthropology 2023 10 23

11 Dev M Rajasthan Engineering Geography 2023 11 28

12 Pooja F WB Medical PSIR 2023 12 25

13 Karan M Bihar Arts History 2023 13 27

14 Tanya F Jharkhand Science Sociology 2024 14 24

15 Rakesh M AP Engineering Anthropology 2024 15 29

16 Divya F Telangana Arts Geography 2024 16 26

17 Yash M Odisha Law PSIR 2024 17 25

;

run;

proc print;run;

Output:

Obs Topper_ID Name Gender State Background Optional_Subject Year Rank Age
1 1 Aarav M Delhi Engineering PSIR 2020 1 26
2 2 Sneha F Maharashtra Medical Sociology 2020 2 25
3 3 Raj M TamilNadu Arts Geography 2021 3 27
4 4 Kavya F Kerala Engineering Anthropology 2021 4 24
5 5 Aditya M UP Commerce PSIR 2021 5 28
6 6 Meera F Karnataka Arts History 2022 6 25
7 7 Rohit M Punjab Law PSIR 2022 7 26
8 8 Priya F Haryana Engineering Geography 2022 8 24
9 9 Arjun M Gujarat Science Sociology 2023 9 27
10 10 Isha F MP Commerce Anthropology 2023 10 23
11 11 Dev M Rajasthan Engineering Geography 2023 11 28
12 12 Pooja F WB Medical PSIR 2023 12 25
13 13 Karan M Bihar Arts History 2023 13 27
14 14 Tanya F Jharkhand Science Sociology 2024 14 24
15 15 Rakesh M AP Engineering Anthropology 2024 15 29
16 16 Divya F Telangana Arts Geography 2024 16 26
17 17 Yash M Odisha Law PSIR 2024 17 25


Step 2: Basic Data Inspection Using PROC PRINT

proc print data=IPS_Toppers;

    title "List of IPS Toppers with Full Details";

run;

Output:

List of IPS Toppers with Full Details

Obs Topper_ID Name Gender State Background Optional_Subject Year Rank Age
1 1 Aarav M Delhi Engineering PSIR 2020 1 26
2 2 Sneha F Maharashtra Medical Sociology 2020 2 25
3 3 Raj M TamilNadu Arts Geography 2021 3 27
4 4 Kavya F Kerala Engineering Anthropology 2021 4 24
5 5 Aditya M UP Commerce PSIR 2021 5 28
6 6 Meera F Karnataka Arts History 2022 6 25
7 7 Rohit M Punjab Law PSIR 2022 7 26
8 8 Priya F Haryana Engineering Geography 2022 8 24
9 9 Arjun M Gujarat Science Sociology 2023 9 27
10 10 Isha F MP Commerce Anthropology 2023 10 23
11 11 Dev M Rajasthan Engineering Geography 2023 11 28
12 12 Pooja F WB Medical PSIR 2023 12 25
13 13 Karan M Bihar Arts History 2023 13 27
14 14 Tanya F Jharkhand Science Sociology 2024 14 24
15 15 Rakesh M AP Engineering Anthropology 2024 15 29
16 16 Divya F Telangana Arts Geography 2024 16 26
17 17 Yash M Odisha Law PSIR 2024 17 25

Step 3: Summary Statistics Using PROC MEANS

proc means data=IPS_Toppers n mean min max;

    var Rank Age;

    title "Summary Statistics of Rank and Age of IPS Toppers";

run;

Output:

Summary Statistics of Rank and Age of IPS Toppers

The MEANS Procedure

Variable N Mean Minimum Maximum
Rank
Age
17
17
9.0000000
25.8235294
1.0000000
23.0000000
17.0000000
29.0000000

Step 4: Frequency Distribution Using PROC FREQ

proc freq data=IPS_Toppers;

    tables Gender State Background Optional_Subject;

    title "Frequency of IPS Toppers by Various Categorical Variables";

run;

Output:

Frequency of IPS Toppers by Various Categorical Variables

The FREQ Procedure

Gender Frequency Percent Cumulative
Frequency
Cumulative
Percent
F 8 47.06 8 47.06
M 9 52.94 17 100.00

State Frequency Percent Cumulative
Frequency
Cumulative
Percent
AP 1 5.88 1 5.88
Bihar 1 5.88 2 11.76
Delhi 1 5.88 3 17.65
Gujarat 1 5.88 4 23.53
Haryana 1 5.88 5 29.41
Jharkhand 1 5.88 6 35.29
Karnataka 1 5.88 7 41.18
Kerala 1 5.88 8 47.06
MP 1 5.88 9 52.94
Maharashtra 1 5.88 10 58.82
Odisha 1 5.88 11 64.71
Punjab 1 5.88 12 70.59
Rajasthan 1 5.88 13 76.47
TamilNadu 1 5.88 14 82.35
Telangana 1 5.88 15 88.24
UP 1 5.88 16 94.12
WB 1 5.88 17 100.00

Background Frequency Percent Cumulative
Frequency
Cumulative
Percent
Arts 4 23.53 4 23.53
Commerce 2 11.76 6 35.29
Engineering 5 29.41 11 64.71
Law 2 11.76 13 76.47
Medical 2 11.76 15 88.24
Science 2 11.76 17 100.00

Optional_Subject Frequency Percent Cumulative
Frequency
Cumulative
Percent
Anthropology 3 17.65 3 17.65
Geography 4 23.53 7 41.18
History 2 11.76 9 52.94
PSIR 5 29.41 14 82.35
Sociology 3 17.65 17 100.00

Step 5: Using PROC SQL to Analyze Trends

5.1: Top States Producing Toppers

proc sql;

    create table Top_States as

    select State, count(*) as No_of_Toppers

    from IPS_Toppers

    group by State

    order by No_of_Toppers desc;

quit;

proc print data=Top_States;

    title "Top States by Number of IPS Toppers";

run;

Output:

Top States by Number of IPS Toppers

Obs State No_of_Toppers
1 Punjab 1
2 Maharashtra 1
3 Jharkhand 1
4 TamilNadu 1
5 MP 1
6 Haryana 1
7 Odisha 1
8 Delhi 1
9 Rajasthan 1
10 Karnataka 1
11 Telangana 1
12 WB 1
13 AP 1
14 Bihar 1
15 Gujarat 1
16 Kerala 1
17 UP 1

5.2: Most Common Optional Subjects

proc sql;

    create table Popular_Subjects as

    select Optional_Subject, count(*) as Frequency

    from IPS_Toppers

    group by Optional_Subject

    order by Frequency desc;

quit;

proc print data=Popular_Subjects;

    title "Most Preferred Optional Subjects";

run;

Output:

Most Preferred Optional Subjects

Obs Optional_Subject Frequency
1 PSIR 5
2 Geography 4
3 Anthropology 3
4 Sociology 3
5 History 2

5.3: Average Rank by Background

proc sql;

    create table Rank_By_Background as

    select Background, avg(Rank) as Avg_Rank format=8.2

    from IPS_Toppers

    group by Background;

quit;

proc print data=Rank_By_Background;

    title "Average Rank Based on Background";

run;

Output:

Average Rank Based on Background

Obs Background Avg_Rank
1 Arts 9.50
2 Commerce 7.50
3 Engineering 7.80
4 Law 12.00
5 Medical 7.00
6 Science 11.50

Step 6: Create Macro for Dynamic Filtering

Define Macro: %TopperByState

%macro TopperByState(state);

    proc sql;

        create table Toppers_&state as

        select * 

        from IPS_Toppers

        where upcase(State) = upcase("&state");

    quit;

    proc print data=Toppers_&state;

        title "Toppers from &state";

    run;

%mend;

%TopperByState(Karnataka)

Output:

Toppers from Karnataka

Obs Topper_ID Name Gender State Background Optional_Subject Year Rank Age
1 6 Meera F Karnataka Arts History 2022 6 25

%TopperByState(UP)

Output:

Toppers from UP

Obs Topper_ID Name Gender State Background Optional_Subject Year Rank Age
1 5 Aditya M UP Commerce PSIR 2021 5 28

%TopperByState(Telangana)

Output:

Toppers from Telangana

Obs Topper_ID Name Gender State Background Optional_Subject Year Rank Age
1 16 Divya F Telangana Arts Geography 2024 16 26

Step 7: Macro to Analyze Gender Gap

Define Macro: %GenderGap

%macro GenderGap;

    proc sql;

        create table Gender_Stats as

        select Gender, count(*) as Count

        from IPS_Toppers

        group by Gender;

    quit;

    proc print data=Gender_Stats;

        title "Gender Distribution Among IPS Toppers";

    run;

%mend;


%GenderGap;

Output:

Gender Distribution Among IPS Toppers

Obs Gender Count
1 F 8
2 M 9

Step 8: Macro Loop to Show Background Trends

Loop Macro: %BackgroundStats

%macro BackgroundStats;

    %let backgrounds = Engineering Medical Arts Commerce Law Science;


    %do i=1 %to 6;

        %let bg = %scan(&backgrounds, &i);


        proc sql;

            create table Stats_&bg as

            select Year, count(*) as Toppers_Count

            from IPS_Toppers

            where Background = "&bg"

            group by Year;

        quit;


        proc print data=Stats_&bg;

            title "Yearly Toppers from &bg Background";

        run;

    %end;

%mend;


%BackgroundStats;

Output:

Yearly Toppers from Engineering Background

Obs Year Toppers_Count
1 2020 1
2 2021 1
3 2022 1
4 2023 1
5 2024 1

Yearly Toppers from Medical Background

Obs Year Toppers_Count
1 2020 1
2 2023 1

Yearly Toppers from Arts Background

Obs Year Toppers_Count
1 2021 1
2 2022 1
3 2023 1
4 2024 1

Yearly Toppers from Commerce Background

Obs Year Toppers_Count
1 2021 1
2 2023 1

Yearly Toppers from Law Background

Obs Year Toppers_Count
1 2022 1
2 2024 1

Yearly Toppers from Science Background

Obs Year Toppers_Count
1 2023 1
2 2024 1

Step 9: Advanced SQL – Multi-Condition Filter

proc sql;

    select Name, Gender, State, Optional_Subject, Rank

    from IPS_Toppers

    where Gender = 'F' and Rank <= 10 and Background = 'Engineering'

    order by Rank;

quit;

Output:

Name Gender State Optional_Subject Rank
Kavya F Kerala Anthropology 4
Priya F Haryana Geography 8

Step 10: Use of Macro Variables for Dynamic Ranking

%let top_rank = 5;


proc sql;

    create table Top_&top_rank as

    select * from IPS_Toppers

    where Rank <= &top_rank

    order by Rank;

quit;


proc print data=Top_&top_rank;

    title "Top &top_rank IPS Toppers";

run;

Output:

Top 5 IPS Toppers

Obs Topper_ID Name Gender State Background Optional_Subject Year Rank Age
1 1 Aarav M Delhi Engineering PSIR 2020 1 26
2 2 Sneha F Maharashtra Medical Sociology 2020 2 25
3 3 Raj M TamilNadu Arts Geography 2021 3 27
4 4 Kavya F Kerala Engineering Anthropology 2021 4 24
5 5 Aditya M UP Commerce PSIR 2021 5 28

Step 11: Export to CSV

proc export data=IPS_Toppers

    outfile="IPS_Toppers.csv"

    dbms=csv

    replace;

run;

Log:

NOTE: The file 'IPS_Toppers.csv' is:
      Filename=C:\sas folder\SASFoundation\9.4\IPS_Toppers.csv,
      RECFM=V,LRECL=32767,File Size (bytes)=0,
      Last Modified=14 June 2025 16:23:04,
      Create Time=14 June 2025 16:23:04

NOTE: 18 records were written to the file 'IPS_Toppers.csv'.
      The minimum record length was 35.
      The maximum record length was 69.
NOTE: There were 17 observations read from the data set WORK.IPS_TOPPERS.
NOTE: DATA statement used (Total process time):
      real time           0.21 seconds
      cpu time            0.06 seconds


17 records created in IPS_Toppers.csv from IPS_TOPPERS.


NOTE: "IPS_Toppers.csv" file was successfully created.
NOTE: PROCEDURE EXPORT used (Total process time):
      real time           2.67 seconds
      cpu time            0.23 seconds



To Visit My Previous First. Last. Statements:Click Here
To Visit My Previous Set Where Statements:Click Here
To Visit My Previous Online Gaming Dataset:Click Here
To Visit My Previous Length Statement Using In Many Ways:Click Here





--->PLEASE FOLLOW OUR BLOG FOR MORE INFORMATION.
--->PLEASE DO COMMENTS AND SHARE OUR BLOG.

PLEASE FOLLOW OUR TELEGRAM CHANNEL CLICK HERE

PLEASE FOLLOW OUR FACEBOOK PAGE  CLICK HERE

PLEASE FOLLOW OUR INSTAGRAM PAGE CLICK HERE


Comments