<?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: Grouping by flag in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Grouping-by-flag/m-p/624699#M184062</link>
    <description>&lt;P&gt;I think if you display your want in a clearer form:&lt;/P&gt;
&lt;PRE&gt;VarA       Flag      GroupRank_VARA
10           0                1
12           0                2
25           0                3
 3           1                1
15           1                2
&lt;/PRE&gt;
&lt;P&gt;You want:&lt;/P&gt;
&lt;PRE&gt;proc rank data=have out= want groups=10; 
   by Flag; 
   var VarA; 
   ranks GroupRank_VarA; 

run;
&lt;/PRE&gt;
&lt;P&gt;Note the S in RANK&lt;STRONG&gt;S.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Your code would have been throwing errors that maybe you should start with.&lt;/P&gt;
&lt;P&gt;And as with all BY statements you have to sort the data by the variables on the BY statement before use.&lt;/P&gt;
&lt;P&gt;Note that Proc Rank will create ranks of 0 to 9 when requesting groups=10. So if you really must have 1 to 10 you will need to take a pass through a data step to add 1.&lt;/P&gt;
&lt;P&gt;From the documentation for Proc Rank&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;H5 class="xis-option"&gt;&lt;A href="http://127.0.0.1:61558/help/proc.hlp/p16s2o8e4bnqrin1phywxdaxqba7.htm#n0ap7rle6oq6fon1209x0i7085jq" target="_blank" rel="noopener"&gt;GROUPS=&lt;SPAN class="xis-userSuppliedValue"&gt;number-of-groups&lt;/SPAN&gt;&lt;/A&gt;&lt;/H5&gt;
&lt;P class="xis-shortDescription"&gt;assigns group values ranging from 0 to &lt;SPAN class="xis-userSuppliedValue"&gt;number-of-groups&lt;/SPAN&gt; minus 1.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
    <pubDate>Thu, 13 Feb 2020 23:47:22 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-02-13T23:47:22Z</dc:date>
    <item>
      <title>Grouping by flag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grouping-by-flag/m-p/624412#M183947</link>
      <description>&lt;P&gt;Dear All:&lt;/P&gt;&lt;P&gt;&amp;nbsp; I want to create 10 groups for the variable VARA using the 0/1 flag.&amp;nbsp; So my data is as follows&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;VarA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Flag&lt;/P&gt;&lt;P&gt;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;24&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;35&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and so forth&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to group the data by VARA and the flag.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I want&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;VarA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Flag&amp;nbsp; &amp;nbsp;Group_FLAG_0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Group_FLAG_1&lt;/P&gt;&lt;P&gt;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;24&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;35&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can this be done in one step.&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Randy&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2020 05:11:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grouping-by-flag/m-p/624412#M183947</guid>
      <dc:creator>RandyStan</dc:creator>
      <dc:date>2020-02-13T05:11:35Z</dc:date>
    </item>
    <item>
      <title>Re: Grouping by flag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grouping-by-flag/m-p/624417#M183950</link>
      <description>&lt;P&gt;Maybe it can be done in one step, but since you have not revealed the logic to be applied in an (for me) understandable way, i can't give you any details on how to do it. And, as&amp;nbsp; side-note, you should start posting data in usable form, so that there is one thing less we need to guess.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2020 06:06:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grouping-by-flag/m-p/624417#M183950</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-02-13T06:06:45Z</dc:date>
    </item>
    <item>
      <title>Re: Grouping by flag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grouping-by-flag/m-p/624493#M183982</link>
      <description>&lt;P&gt;What values would be assigned to Group_FLAG_0 and Group_FLAG_1?&lt;/P&gt;
&lt;P&gt;What are the rules for assigning the values to the variables?&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2020 15:25:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grouping-by-flag/m-p/624493#M183982</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-02-13T15:25:26Z</dc:date>
    </item>
    <item>
      <title>Re: Grouping by flag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grouping-by-flag/m-p/624507#M183990</link>
      <description>&lt;P&gt;How do you want them grouped? What's the logic?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input VarA Flag;
	datalines;
10 0
8  0
11 1
24 1
35 0
	;
run;
proc sort data = have; by VarA; run;
data want;
	set have;
	by varA;
	if Flag = 0 then Group_FLAG_0 = 1;
	else if Flag = 1 then Group_FLAG_1 = 1; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2020 15:57:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grouping-by-flag/m-p/624507#M183990</guid>
      <dc:creator>Krueger</dc:creator>
      <dc:date>2020-02-13T15:57:19Z</dc:date>
    </item>
    <item>
      <title>Re: Grouping by flag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grouping-by-flag/m-p/624655#M184048</link>
      <description>&lt;P&gt;If I may, let me try to explain&lt;/P&gt;&lt;P&gt;I have varA with numbers and a Flag (0,1)&lt;/P&gt;&lt;P&gt;I want to construct a decile column (which gives the decile for VarA when Flag = 0; and when Flag = 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So for example; (A shortened version of the data set)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;VarA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Flag&lt;/P&gt;&lt;P&gt;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;&amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;15&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I construct a group_Rank variable such that&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;VarA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Flag&amp;nbsp; &amp;nbsp; &amp;nbsp; GroupRank_VARA&lt;/P&gt;&lt;P&gt;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;15&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;&amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;proc&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;rank&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=have&lt;/SPAN&gt; out&lt;SPAN class="token operator"&gt;= want &lt;/SPAN&gt;groups&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;10&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; Flag&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;var&lt;/SPAN&gt; VarA&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;rank&lt;/SPAN&gt;&amp;nbsp;GroupRank_VarA&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token punctuation"&gt;I hope I am clearer this time.&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token punctuation"&gt;Randy&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2020 21:04:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grouping-by-flag/m-p/624655#M184048</guid>
      <dc:creator>RandyStan</dc:creator>
      <dc:date>2020-02-13T21:04:57Z</dc:date>
    </item>
    <item>
      <title>Re: Grouping by flag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grouping-by-flag/m-p/624699#M184062</link>
      <description>&lt;P&gt;I think if you display your want in a clearer form:&lt;/P&gt;
&lt;PRE&gt;VarA       Flag      GroupRank_VARA
10           0                1
12           0                2
25           0                3
 3           1                1
15           1                2
&lt;/PRE&gt;
&lt;P&gt;You want:&lt;/P&gt;
&lt;PRE&gt;proc rank data=have out= want groups=10; 
   by Flag; 
   var VarA; 
   ranks GroupRank_VarA; 

run;
&lt;/PRE&gt;
&lt;P&gt;Note the S in RANK&lt;STRONG&gt;S.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Your code would have been throwing errors that maybe you should start with.&lt;/P&gt;
&lt;P&gt;And as with all BY statements you have to sort the data by the variables on the BY statement before use.&lt;/P&gt;
&lt;P&gt;Note that Proc Rank will create ranks of 0 to 9 when requesting groups=10. So if you really must have 1 to 10 you will need to take a pass through a data step to add 1.&lt;/P&gt;
&lt;P&gt;From the documentation for Proc Rank&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;H5 class="xis-option"&gt;&lt;A href="http://127.0.0.1:61558/help/proc.hlp/p16s2o8e4bnqrin1phywxdaxqba7.htm#n0ap7rle6oq6fon1209x0i7085jq" target="_blank" rel="noopener"&gt;GROUPS=&lt;SPAN class="xis-userSuppliedValue"&gt;number-of-groups&lt;/SPAN&gt;&lt;/A&gt;&lt;/H5&gt;
&lt;P class="xis-shortDescription"&gt;assigns group values ranging from 0 to &lt;SPAN class="xis-userSuppliedValue"&gt;number-of-groups&lt;/SPAN&gt; minus 1.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Thu, 13 Feb 2020 23:47:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grouping-by-flag/m-p/624699#M184062</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-02-13T23:47:22Z</dc:date>
    </item>
  </channel>
</rss>

