BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kggunes
Calcite | Level 5

Hello,

For each observation I would like to create a variable showing the percentage of the total observation. To put it simply, let's say I have 100 observations and i want to see the "8%" in the 8th observation (kind of percentiles).

I am aware of Proc UNIVARIATE;  PCTLPTS= but couldn't find a good example application of it :smileyplain: Could anyone suggest an easy solution?

Many thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

Is this what you want?

data calss;

  set sashelp.class nobs=nobs;

  per=_n_/nobs;

  format per percent6.2;

run;

proc print;run;

Obs    Name       Sex    Age    Height    Weight       per

              1    Alfred      M      14     69.0      112.5     5.3%

              2    Alice       F      13     56.5       84.0      11%

              3    Barbara     F      13     65.3       98.0      16%

              4    Carol       F      14     62.8      102.5      21%

              5    Henry       M      14     63.5      102.5      26%

              6    James       M      12     57.3       83.0      32%

              7    Jane        F      12     59.8       84.5      37%

              8    Janet       F      15     62.5      112.5      42%

              9    Jeffrey     M      13     62.5       84.0      47%

             10    John        M      12     59.0       99.5      53%

             11    Joyce       F      11     51.3       50.5      58%

             12    Judy        F      14     64.3       90.0      63%

             13    Louise      F      12     56.3       77.0      68%

             14    Mary        F      15     66.5      112.0      74%

             15    Philip      M      16     72.0      150.0      79%

             16    Robert      M      12     64.8      128.0      84%

             17    Ronald      M      15     67.0      133.0      89%

             18    Thomas      M      11     57.5       85.0      95%

             19    William     M      15     66.5      112.0     100%

View solution in original post

5 REPLIES 5
Linlin
Lapis Lazuli | Level 10

Is this what you want?

data calss;

  set sashelp.class nobs=nobs;

  per=_n_/nobs;

  format per percent6.2;

run;

proc print;run;

Obs    Name       Sex    Age    Height    Weight       per

              1    Alfred      M      14     69.0      112.5     5.3%

              2    Alice       F      13     56.5       84.0      11%

              3    Barbara     F      13     65.3       98.0      16%

              4    Carol       F      14     62.8      102.5      21%

              5    Henry       M      14     63.5      102.5      26%

              6    James       M      12     57.3       83.0      32%

              7    Jane        F      12     59.8       84.5      37%

              8    Janet       F      15     62.5      112.5      42%

              9    Jeffrey     M      13     62.5       84.0      47%

             10    John        M      12     59.0       99.5      53%

             11    Joyce       F      11     51.3       50.5      58%

             12    Judy        F      14     64.3       90.0      63%

             13    Louise      F      12     56.3       77.0      68%

             14    Mary        F      15     66.5      112.0      74%

             15    Philip      M      16     72.0      150.0      79%

             16    Robert      M      12     64.8      128.0      84%

             17    Ronald      M      15     67.0      133.0      89%

             18    Thomas      M      11     57.5       85.0      95%

             19    William     M      15     66.5      112.0     100%

kggunes
Calcite | Level 5

Hello Linlin,

Thank you for the prompt and quick answer! If I may ask one more thing to enhance the question.. what if I want to see the percent by group - in the same output dataset? For this example, how can we group the percent answer for males and females separately?

Linlin
Lapis Lazuli | Level 10

Hi,

Is this what you want?

data calss;
do until(done1);
  set sashelp.class end=done1;
  if upcase(sex)='F' then girls+1;
    else boys+1;
  end;
  do until(done2);
   set sashelp.class end=done2 nobs=nobs;
  if upcase(sex)='F' then g+1;
    else b+1;
n+1;
  percent_t=n/nobs;
  percent_f=g/girls;
  percen_m=b/boys;
  output;
end;
  format per: percent8.2;
run;
proc print;run;                                        percent_  percent_
   Obs  Name     Sex  Age  Height  Weight  girls  boys  g   b   n     t         f      percen_m

     1  Alfred    M    14   69.0    112.5    9     10   0   1   1    5.3%      .00%       10%
     2  Alice     F    13   56.5     84.0    9     10   1   1   2     11%       11%       10%
     3  Barbara   F    13   65.3     98.0    9     10   2   1   3     16%       22%       10%
     4  Carol     F    14   62.8    102.5    9     10   3   1   4     21%       33%       10%
     5  Henry     M    14   63.5    102.5    9     10   3   2   5     26%       33%       20%
     6  James     M    12   57.3     83.0    9     10   3   3   6     32%       33%       30%
     7  Jane      F    12   59.8     84.5    9     10   4   3   7     37%       44%       30%
     8  Janet     F    15   62.5    112.5    9     10   5   3   8     42%       56%       30%
     9  Jeffrey   M    13   62.5     84.0    9     10   5   4   9     47%       56%       40%
    10  John      M    12   59.0     99.5    9     10   5   5  10     53%       56%       50%
    11  Joyce     F    11   51.3     50.5    9     10   6   5  11     58%       67%       50%
    12  Judy      F    14   64.3     90.0    9     10   7   5  12     63%       78%       50%
    13  Louise    F    12   56.3     77.0    9     10   8   5  13     68%       89%       50%
    14  Mary      F    15   66.5    112.0    9     10   9   5  14     74%      100%       50%
    15  Philip    M    16   72.0    150.0    9     10   9   6  15     79%      100%       60%
    16  Robert    M    12   64.8    128.0    9     10   9   7  16     84%      100%       70%
    17  Ronald    M    15   67.0    133.0    9     10   9   8  17     89%      100%       80%
    18  Thomas    M    11   57.5     85.0    9     10   9   9  18     95%      100%       90%
    19  William   M    15   66.5    112.0    9     10   9  10  19    100%      100%      100%

kggunes
Calcite | Level 5

Awesome.. Thanks!

TomKari
Onyx | Level 15

Here's another option:

data work.class;
set sashelp.class;
varseq = _n_;
run;

proc sort data=work.class;
by sex;

proc rank data = work.class groups=100 ties=mean out=work.rankedclass;
by sex;
var varseq;
ranks varseq_rank;
run;

Tom

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1883 views
  • 6 likes
  • 3 in conversation