<?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: CODING FOR FIRST VARIABLE BY GROUP in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/CODING-FOR-FIRST-VARIABLE-BY-GROUP/m-p/865874#M341948</link>
    <description>&lt;P&gt;It is easier if you post your example data as a datastep, e.g. like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;                                                                                                                              
input id $ year FD REPORTING;                                                                                                           
cards;                                                                                                                                  
a 1999 0 0                                                                                                                              
a 2000 0 0                                                                                                                              
a 2001 1 0                                                                                                                              
a 2002 1 0                                                                                                                              
a 2003 0 1                                                                                                                              
a 2004 0 1                                                                                                                              
a 2005 0 1                                                                                                                              
b 2001 0 0                                                                                                                              
b 2002 0 0                                                                                                                              
b 2003 0 0                                                                                                                              
b 2004 0 1                                                                                                                              
b 2005 0 1                                                                                                                              
c 1998 0 0                                                                                                                              
c 1999 0 0                                                                                                                              
c 2000 1 0                                                                                                                              
c 2001 0 0                                                                                                                              
c 2002 0 0                                                                                                                              
;run;              
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I think you can get what you want like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;                                                                                                                              
  _FD=0;                                                                                                                                
  _report=0;                                                                                                                            
  do until(last.id);                                                                                                                    
    set have;                                                                                                                           
    by id;                                                                                                                              
    if fd and not _fd then do;                                                                                                          
      NEW_FD=1;                                                                                                                         
      _FD=1;                                                                                                                            
      end;                                                                                                                              
    else NEW_FD=0;                                                                                                                      
    if reporting and not _report then do;                                                                                               
      NEW_REPORTING=1;                                                                                                                  
      _report=1;                                                                                                                        
      end;                                                                                                                              
    else NEW_REPORTING=0;                                                                                                               
    output;                                                                                                                             
    end;                                                                                                                                
drop _:;                                                                                                                                
run;        
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 23 Mar 2023 09:27:27 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2023-03-23T09:27:27Z</dc:date>
    <item>
      <title>CODING FOR FIRST VARIABLE BY GROUP</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CODING-FOR-FIRST-VARIABLE-BY-GROUP/m-p/865871#M341947</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;my data looks like that:&amp;nbsp;&lt;/P&gt;&lt;P&gt;id year FD REPORTING&lt;BR /&gt;a 1999 0 0&lt;BR /&gt;a 2000 0 0&lt;BR /&gt;a 2001 1 0&lt;BR /&gt;a 2002 1 0&lt;BR /&gt;a 2003 0 1&lt;BR /&gt;a 2004 0 1&lt;BR /&gt;a 2005 0 1&lt;BR /&gt;b 2001 0 0&lt;BR /&gt;b 2002 0 0&lt;BR /&gt;b 2003 0 0&lt;BR /&gt;b 2004 0 1&lt;BR /&gt;b 2005 0 1&lt;BR /&gt;c 1998 0 0&lt;BR /&gt;c 1999 0 0&lt;BR /&gt;c 2000 1 0&lt;BR /&gt;c 2001 0 0&lt;BR /&gt;c 2002 0 0&lt;/P&gt;&lt;P&gt;I want to create new variables that equal 1 only for the first reporting=1 and first fd=1. Meaning- I want my data to look like that:&amp;nbsp;&lt;/P&gt;&lt;P&gt;id year FD REPORTING NEW_FD NEW_REPORTING&lt;BR /&gt;a 1999 0 0 0 0&lt;BR /&gt;a 2000 0 0 0 0&lt;BR /&gt;a 2001 1 0 1 0&lt;BR /&gt;a 2002 1 0 0 0&lt;BR /&gt;a 2003 0 1 0 1&lt;BR /&gt;a 2004 0 1 0 0&lt;BR /&gt;a 2005 0 1 0 0&lt;BR /&gt;b 2001 0 0 0 0&lt;BR /&gt;b 2002 0 0 0 0&lt;BR /&gt;b 2003 0 0 0 0&lt;BR /&gt;b 2004 0 1 0 1&lt;BR /&gt;b 2005 0 1 0 0&lt;BR /&gt;c 1998 0 0 0 0&lt;BR /&gt;c 1999 0 0 0 0&lt;BR /&gt;c 2000 1 0 1 0&lt;BR /&gt;c 2001 0 0 0 0&lt;BR /&gt;c 2002 0 0 0 0&lt;/P&gt;&lt;P&gt;Thanks!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2023 08:58:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CODING-FOR-FIRST-VARIABLE-BY-GROUP/m-p/865871#M341947</guid>
      <dc:creator>HEB1</dc:creator>
      <dc:date>2023-03-23T08:58:54Z</dc:date>
    </item>
    <item>
      <title>Re: CODING FOR FIRST VARIABLE BY GROUP</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CODING-FOR-FIRST-VARIABLE-BY-GROUP/m-p/865874#M341948</link>
      <description>&lt;P&gt;It is easier if you post your example data as a datastep, e.g. like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;                                                                                                                              
input id $ year FD REPORTING;                                                                                                           
cards;                                                                                                                                  
a 1999 0 0                                                                                                                              
a 2000 0 0                                                                                                                              
a 2001 1 0                                                                                                                              
a 2002 1 0                                                                                                                              
a 2003 0 1                                                                                                                              
a 2004 0 1                                                                                                                              
a 2005 0 1                                                                                                                              
b 2001 0 0                                                                                                                              
b 2002 0 0                                                                                                                              
b 2003 0 0                                                                                                                              
b 2004 0 1                                                                                                                              
b 2005 0 1                                                                                                                              
c 1998 0 0                                                                                                                              
c 1999 0 0                                                                                                                              
c 2000 1 0                                                                                                                              
c 2001 0 0                                                                                                                              
c 2002 0 0                                                                                                                              
;run;              
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I think you can get what you want like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;                                                                                                                              
  _FD=0;                                                                                                                                
  _report=0;                                                                                                                            
  do until(last.id);                                                                                                                    
    set have;                                                                                                                           
    by id;                                                                                                                              
    if fd and not _fd then do;                                                                                                          
      NEW_FD=1;                                                                                                                         
      _FD=1;                                                                                                                            
      end;                                                                                                                              
    else NEW_FD=0;                                                                                                                      
    if reporting and not _report then do;                                                                                               
      NEW_REPORTING=1;                                                                                                                  
      _report=1;                                                                                                                        
      end;                                                                                                                              
    else NEW_REPORTING=0;                                                                                                               
    output;                                                                                                                             
    end;                                                                                                                                
drop _:;                                                                                                                                
run;        
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Mar 2023 09:27:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CODING-FOR-FIRST-VARIABLE-BY-GROUP/m-p/865874#M341948</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-03-23T09:27:27Z</dc:date>
    </item>
    <item>
      <title>Re: CODING FOR FIRST VARIABLE BY GROUP</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CODING-FOR-FIRST-VARIABLE-BY-GROUP/m-p/865877#M341949</link>
      <description>It works great! Many thanks &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;</description>
      <pubDate>Thu, 23 Mar 2023 09:38:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CODING-FOR-FIRST-VARIABLE-BY-GROUP/m-p/865877#M341949</guid>
      <dc:creator>HEB1</dc:creator>
      <dc:date>2023-03-23T09:38:15Z</dc:date>
    </item>
  </channel>
</rss>

