<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Number of observations in each percentile in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observations-in-each-percentile/m-p/557638#M155477</link>
    <description>&lt;P&gt;I think that you may want Proc Rank for this specific task;&lt;/P&gt;
&lt;PRE&gt;proc rank  data= mydataset groups=100
    out=rankedset;
   by age;
   var var2;
   ranks varrank;
   ;
run;&lt;/PRE&gt;
&lt;P&gt;Will add a variable VARRANK to the data which indicates which percentile the record belongs to.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then use proc freq/report/tabulate to count age values by the varrank values.&lt;/P&gt;</description>
    <pubDate>Thu, 09 May 2019 22:55:34 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2019-05-09T22:55:34Z</dc:date>
    <item>
      <title>Number of observations in each percentile</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observations-in-each-percentile/m-p/557635#M155474</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am new to SAS and I struggle to find a way for my problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data set having variables:&lt;/P&gt;&lt;P&gt;va1 var2&amp;nbsp; age&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I calculated the percentiles using the code below and this worked well:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc univatiate data=mydataset;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by age;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; weight var1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var var2;&lt;/P&gt;&lt;P&gt;output out = mynew_data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PctlPre=PERC_&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PctPts=0 to 100 by 1;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I now would like to determine how many observations there are within each percentile and I am really struggling. I need to have the output to be&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; p1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; p2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; etc...&lt;/P&gt;&lt;P&gt;age1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n obs&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n obs&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;age2&lt;/P&gt;&lt;P&gt;etc..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your help would be much appreciated.&lt;/P&gt;</description>
      <pubDate>Thu, 09 May 2019 22:38:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observations-in-each-percentile/m-p/557635#M155474</guid>
      <dc:creator>PSP_1</dc:creator>
      <dc:date>2019-05-09T22:38:03Z</dc:date>
    </item>
    <item>
      <title>Re: Number of observations in each percentile</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observations-in-each-percentile/m-p/557638#M155477</link>
      <description>&lt;P&gt;I think that you may want Proc Rank for this specific task;&lt;/P&gt;
&lt;PRE&gt;proc rank  data= mydataset groups=100
    out=rankedset;
   by age;
   var var2;
   ranks varrank;
   ;
run;&lt;/PRE&gt;
&lt;P&gt;Will add a variable VARRANK to the data which indicates which percentile the record belongs to.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then use proc freq/report/tabulate to count age values by the varrank values.&lt;/P&gt;</description>
      <pubDate>Thu, 09 May 2019 22:55:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observations-in-each-percentile/m-p/557638#M155477</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-05-09T22:55:34Z</dc:date>
    </item>
    <item>
      <title>Re: Number of observations in each percentile</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observations-in-each-percentile/m-p/557639#M155478</link>
      <description>PROC RANK doesn't have a WEIGHT statement so the percentiles wouldn't be calculated correctly &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;</description>
      <pubDate>Thu, 09 May 2019 23:00:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observations-in-each-percentile/m-p/557639#M155478</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-05-09T23:00:56Z</dc:date>
    </item>
    <item>
      <title>Re: Number of observations in each percentile</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observations-in-each-percentile/m-p/557674#M155506</link>
      <description>&lt;P&gt;Here's one way - please read the comments this will need some tweaking to ensure you get the correct values.&lt;/P&gt;
&lt;P&gt;You didn't provide sample data so I worked with the Stocks data available, replace with your data set and variable names and you should be fine.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;*add weights to fake data;

data stocks;
    call streaminit(20);
    set sashelp.stocks;
    myWeight=rand('integer', 1, 100);
run;

*get percentiles;

proc univariate data=stocks noprint;
    by stock;
    weight myWeight;
    var open;
    output out=myNewData PctlPre=PERC_ PctlPts=0 to 100 by 1;
run;

*merge into main data and calculate percentile using an array;
*you may want to check the &amp;lt;= its likely not right;
*and the index to see how you want to deal with records above/below;

data calcs;
    merge stocks myNewData;
    by stock;
    array pct(*) perc:;

    do i=2 to dim(pct);

        if pct(i-1) &amp;lt;=open &amp;lt;=pct(i) then
            index=i-1;
    end;
    drop perc:;
run;

*get frequencies;

proc freq data=calcs noprint;
    table stock*index / out=long missing;
run;

*transpose to desired format - if just printing you dont need this
step;

proc transpose data=long out=wide prefix=PRCT_;
    by stock;
    id index;
    var count;
run;

*print for display;

proc print data=wide;
run;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/273554"&gt;@PSP_1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I am new to SAS and I struggle to find a way for my problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a data set having variables:&lt;/P&gt;
&lt;P&gt;va1 var2&amp;nbsp; age&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I calculated the percentiles using the code below and this worked well:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc univatiate data=mydataset;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by age;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; weight var1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var var2;&lt;/P&gt;
&lt;P&gt;output out = mynew_data&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PctlPre=PERC_&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PctPts=0 to 100 by 1;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I now would like to determine how many observations there are within each percentile and I am really struggling. I need to have the output to be&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; p1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; p2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; etc...&lt;/P&gt;
&lt;P&gt;age1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n obs&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n obs&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;age2&lt;/P&gt;
&lt;P&gt;etc..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your help would be much appreciated.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2019 02:33:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observations-in-each-percentile/m-p/557674#M155506</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-05-10T02:33:00Z</dc:date>
    </item>
    <item>
      <title>Re: Number of observations in each percentile</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observations-in-each-percentile/m-p/557828#M155564</link>
      <description>Thank you so, so much! The code worked perfectly well and it solved my problem!&lt;BR /&gt;&lt;BR /&gt;Thanks again for your time!</description>
      <pubDate>Fri, 10 May 2019 15:26:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observations-in-each-percentile/m-p/557828#M155564</guid>
      <dc:creator>PSP_1</dc:creator>
      <dc:date>2019-05-10T15:26:12Z</dc:date>
    </item>
    <item>
      <title>Re: Number of observations in each percentile</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observations-in-each-percentile/m-p/557832#M155568</link>
      <description>It may be helpful to post your solution as well, since you had to tweak it to get the right values.</description>
      <pubDate>Fri, 10 May 2019 15:27:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observations-in-each-percentile/m-p/557832#M155568</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-05-10T15:27:49Z</dc:date>
    </item>
    <item>
      <title>Re: Number of observations in each percentile</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observations-in-each-percentile/m-p/558663#M155926</link>
      <description>Yes, sure.&lt;BR /&gt;&lt;BR /&gt;The only change I made to the code was in the below section:&lt;BR /&gt;&lt;BR /&gt;data calcs;&lt;BR /&gt;merge stocks myNewData;&lt;BR /&gt;by stock;&lt;BR /&gt;array pct(*) perc:;&lt;BR /&gt;&lt;BR /&gt;do i=2 to dim(pct);&lt;BR /&gt;&lt;BR /&gt;if pct(i-1) &amp;lt;=open &amp;lt;=pct(i) then&lt;BR /&gt;&lt;BR /&gt;* on the line below I changed from index = i-1 to index = i-2 otherwise the percentiles were shifted by 1;;&lt;BR /&gt;&lt;BR /&gt;index=i-2;&lt;BR /&gt;end;&lt;BR /&gt;drop perc:;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Apart of one line, everything else I used as is.</description>
      <pubDate>Tue, 14 May 2019 15:48:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observations-in-each-percentile/m-p/558663#M155926</guid>
      <dc:creator>PSP_1</dc:creator>
      <dc:date>2019-05-14T15:48:07Z</dc:date>
    </item>
  </channel>
</rss>

