<?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: Create variable by ID and based on another variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-by-ID-and-based-on-another-variable/m-p/583607#M166144</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID	ID_sub $	Y;*	Z;
cards;
1	A	0	1
1	B	0	1
1	C	1	1
2	A	1	1
2	B	0	2
2	C	0	2
2	D	1	2
3	A	0	1
3	B	0	1
3	C	0	1
3	D	0	1
3	E	0	1
3	F	0	1
3	G	0	1
4	A	0	1
4	B	1	1
4	C	1	2
5	A	1	1
5	B	1	2
;
data want;
if 0 then set have;
 z=1;
 do until(last.id);
 set have;
 by id;
 output;
 if z&amp;lt;2 then z+y;
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 23 Aug 2019 20:59:05 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2019-08-23T20:59:05Z</dc:date>
    <item>
      <title>Create variable by ID and based on another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-by-ID-and-based-on-another-variable/m-p/583599#M166142</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to create the variable Z which takes the value 1 up until (and including when) Y=1 &lt;U&gt;for the first time&lt;/U&gt;. After that, Z takes the value of 2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For the same ID, Y can take the value of 1 at max two times.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please see attached an example of what I need.&lt;/P&gt;&lt;P&gt;Thanks very much!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 20:28:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-by-ID-and-based-on-another-variable/m-p/583599#M166142</guid>
      <dc:creator>blackandwhite</dc:creator>
      <dc:date>2019-08-23T20:28:24Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable by ID and based on another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-by-ID-and-based-on-another-variable/m-p/583607#M166144</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID	ID_sub $	Y;*	Z;
cards;
1	A	0	1
1	B	0	1
1	C	1	1
2	A	1	1
2	B	0	2
2	C	0	2
2	D	1	2
3	A	0	1
3	B	0	1
3	C	0	1
3	D	0	1
3	E	0	1
3	F	0	1
3	G	0	1
4	A	0	1
4	B	1	1
4	C	1	2
5	A	1	1
5	B	1	2
;
data want;
if 0 then set have;
 z=1;
 do until(last.id);
 set have;
 by id;
 output;
 if z&amp;lt;2 then z+y;
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Aug 2019 20:59:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-by-ID-and-based-on-another-variable/m-p/583607#M166144</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-23T20:59:05Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable by ID and based on another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-by-ID-and-based-on-another-variable/m-p/583609#M166145</link>
      <description>&lt;P&gt;Thank you!!&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 21:06:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-by-ID-and-based-on-another-variable/m-p/583609#M166145</guid>
      <dc:creator>blackandwhite</dc:creator>
      <dc:date>2019-08-23T21:06:13Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable by ID and based on another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-by-ID-and-based-on-another-variable/m-p/583619#M166152</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/166182"&gt;@blackandwhite&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Looks like a backtracking task. So, one approach is to read every group of records ending in Y=1 twice, initializing Z=0 at the beginning of each ID by-group and incrementing Z before each second pass:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                 
  input ID ID_sub :$1. Y ;  
  cards ;                   
1  A  0                     
1  B  0                     
1  C  1                     
2  A  1                     
2  B  0                     
2  C  0                     
2  D  1                     
3  A  0                     
3  B  0                     
3  C  0                     
3  D  0                     
3  E  0                     
3  F  0                     
3  G  0                     
4  A  0                     
4  B  1                     
4  C  1                     
5  A  1                     
5  B  1                     
run ;                       
                            
data want ;                 
  do until (Y) ;            
    set have ;              
    by id ;                 
    if first.id then Z = 0 ;
  end ;                     
  Z + 1 ;                   
  do until (Y) ;            
    set have ;              
    output ;                
  end ;                     
run ;                       
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 22:03:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-by-ID-and-based-on-another-variable/m-p/583619#M166152</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-08-23T22:03:43Z</dc:date>
    </item>
  </channel>
</rss>

