<?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: How to create a variable to subgroup observations within a group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-to-subgroup-observations-within-a-group/m-p/643203#M191925</link>
    <description>&lt;P&gt;please try the below code for flag2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input group flag;
	datalines;
101 .
101 .
101 1
101 .
101 1
101 .
101 .
102 .
102 1
102 .
102 .
;

data want;
set have;
by group flag notsorted;
retain new_var;
if first.group then new_var=1;
else new_var+flag;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 27 Apr 2020 06:19:45 GMT</pubDate>
    <dc:creator>Jagadishkatam</dc:creator>
    <dc:date>2020-04-27T06:19:45Z</dc:date>
    <item>
      <title>How to create a variable to subgroup observations within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-to-subgroup-observations-within-a-group/m-p/643200#M191923</link>
      <description>&lt;P&gt;I have a dataset that has multiple groups of observations. There is a variable, flag, which either takes value 1 or missing. I want to create a new variable to divide each group into subgroups based on the value of flag. Whenever flag = 1, start counting a new subgroup. Please see the code below as an example.&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input group flag;
	datalines;
101 .
101 .
101 1
101 .
101 1
101 .
101 .
102 .
102 1
102 .
102 .
;

data want;
	input group flag new_var;
	datalines;
101 . 1
101 . 1
101 1 2
101 . 2
101 1 3
101 . 3
101 . 3
102 . 1
102 1 2
102 . 2
102 . 2
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Apr 2020 05:50:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-to-subgroup-observations-within-a-group/m-p/643200#M191923</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2020-04-27T05:50:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a variable to subgroup observations within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-to-subgroup-observations-within-a-group/m-p/643203#M191925</link>
      <description>&lt;P&gt;please try the below code for flag2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input group flag;
	datalines;
101 .
101 .
101 1
101 .
101 1
101 .
101 .
102 .
102 1
102 .
102 .
;

data want;
set have;
by group flag notsorted;
retain new_var;
if first.group then new_var=1;
else new_var+flag;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Apr 2020 06:19:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-to-subgroup-observations-within-a-group/m-p/643203#M191925</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2020-04-27T06:19:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a variable to subgroup observations within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-to-subgroup-observations-within-a-group/m-p/643204#M191926</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by group;
if first.group
then newvar = 1;
else if flag then newvar + 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Apr 2020 06:11:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-to-subgroup-observations-within-a-group/m-p/643204#M191926</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-27T06:11:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a variable to subgroup observations within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-to-subgroup-observations-within-a-group/m-p/643207#M191927</link>
      <description>&lt;P&gt;If you want a more arcane solution, try:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	by group;
	length new_var 8;
	retain new_var;
		
	new_var = ifn(first.group, 1, new_var) + sum(0, flag);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Apr 2020 07:07:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-to-subgroup-observations-within-a-group/m-p/643207#M191927</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-04-27T07:07:47Z</dc:date>
    </item>
  </channel>
</rss>

