<?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: Loop, Conditional and Incrementation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loop-Conditional-and-Incrementation/m-p/531309#M145400</link>
    <description>&lt;P&gt;Hi Novinisrin and Ballardw !&lt;/P&gt;&lt;P&gt;Thanks for your time and help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tested your scripts and they work fine in sandbox but not on live for a reason I can't find.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The closest result was with ballardw script, in witch the result was working fine but only on individuals that started with a "true" YN variable. It would drop the individuals starting with a "false" statement, except the very first individual SUBJID="3".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The script I executed and the output&amp;nbsp;:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1998  data work.want;
1999     set work.have;
2000     by notsorted SUBJID notsorted CMYN;
2001     retain flag;
2002     if first.name then flag=(CMYN='false');
2003     else do;
2004        if flag and (CMYN='false') then delete;
2005        else flag = (CMYN='false');
2006     end;
2007  run;

NOTE: Variable first.name non initialisée.
NOTE: There were 28 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 5 observations and 4 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It's weird because the script is working fine on sandbox data, whatever combinaison I try.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again for your time !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;EDIT : I just realised first. function has "name" as parameter.. that was the problem.&lt;/P&gt;&lt;P&gt;Your scripts are working fine&amp;nbsp;!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm still learning haha.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help !&lt;/P&gt;</description>
    <pubDate>Wed, 30 Jan 2019 12:35:29 GMT</pubDate>
    <dc:creator>Althaea</dc:creator>
    <dc:date>2019-01-30T12:35:29Z</dc:date>
    <item>
      <title>Loop, Conditional and Incrementation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-Conditional-and-Incrementation/m-p/531072#M145295</link>
      <description>&lt;P&gt;Hi !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've read a lot of subjects about loops and incrementation but I can't figure out how to do what seems like a simple task.&lt;/P&gt;&lt;P&gt;Here's what my data looks like :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.HAVE;
  infile datalines;
  input Name:$10. ID:2. YN:$8. value:$5.;
datalines;
 Antoine 01 true (1)
 Antoine 01 false (2)
 Thomas 02 true (1)
 Thomas 02 true (2)
 Thomas 02 false (3)
 Adeline 03 false (1)
 Michelle 04 true (1)
 Michelle 04 false (2)
 Michelle 04 false (3)
 Michelle 04 false (4)
 Clemence 05 false (1)
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;For each individual, if YN was "true", a new row would be created. A "false" should have stoped the incrementation.&lt;/P&gt;&lt;P&gt;But following this logic,&amp;nbsp;in this data Michelle has 2 rows that shouldn't exist.&lt;/P&gt;&lt;P&gt;My goal is to detect and delete those rows.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Expected result :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.WANT;
  infile datalines;
  input Name:$10. ID:2. YN:$8. value:$5.;
datalines;
 Antoine 01 true (1)
 Antoine 01 false (2)
 Thomas 02 true (1)
 Thomas 02 true (2)
 Thomas 02 false (3)
 Adeline 03 false (1)
 Michelle 04 true (1)
 Michelle 04 false (2)
 Clemence 05 false (1)
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It seems easy but I'm struggling and feel like i'm burning learning steps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My&amp;nbsp;logic was something like that :&lt;/P&gt;&lt;P&gt;IF actualrow.ID = previousrow.ID AND actualrow.YN = previousrow.YN = "false" THEN drop actualrow&lt;/P&gt;&lt;P&gt;Maybe I can use proc sql, but I don't know how to include sql within a data step yet..&lt;/P&gt;&lt;P&gt;I'm also having issues finding documents talking about this from basics.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All help appreciated.&lt;/P&gt;&lt;P&gt;Thanks for your time !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jan 2019 16:48:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-Conditional-and-Incrementation/m-p/531072#M145295</guid>
      <dc:creator>Althaea</dc:creator>
      <dc:date>2019-01-29T16:48:12Z</dc:date>
    </item>
    <item>
      <title>Re: Loop, Conditional and Incrementation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-Conditional-and-Incrementation/m-p/531083#M145299</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.HAVE;
  infile datalines;
  input Name:$10. ID:2. YN:$8. value:$5.;
datalines;
 Antoine 01 true (1)
 Antoine 01 false (2)
 Thomas 02 true (1)
 Thomas 02 true (2)
 Thomas 02 false (3)
 Adeline 03 false (1)
 Michelle 04 true (1)
 Michelle 04 false (2)
 Michelle 04 false (3)
 Michelle 04 false (4)
 Clemence 05 false (1)
;




data want;
set have;
by id yn notsorted;
retain f;
if first.id then f=.;
f=ifn(not first.id and lag(yn)='false',1,.);
if 	not f;
drop f ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;/*or*/&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id yn notsorted;
retain f;
if first.id then f=.;
f=not first.id and lag(yn)='false';
if 	not f;
drop f ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jan 2019 17:25:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-Conditional-and-Incrementation/m-p/531083#M145299</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-29T17:25:56Z</dc:date>
    </item>
    <item>
      <title>Re: Loop, Conditional and Incrementation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-Conditional-and-Incrementation/m-p/531084#M145300</link>
      <description>&lt;P&gt;This works for your example data:&lt;/P&gt;
&lt;PRE&gt;data work.want;
   set work.have;
   by notsorted name notsorted yn;
   retain flag;
   if first.name then flag=(yn='false');
   else do;
      if flag and (yn='false') then delete;
      else flag = (yn='false');
   end;
run;&lt;/PRE&gt;
&lt;P&gt;If name is ever different for the same ID you could use ID in place of Name in the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS Proc SQL does not have a "previous row" feature that works in the way you are thinking.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW if you code this with true/false or yes/no as numeric 1/0 values you don't need to use yn='false' for comparisons. You could use "if yn" to test for true or "if not(yn)" for false&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jan 2019 17:18:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-Conditional-and-Incrementation/m-p/531084#M145300</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-01-29T17:18:38Z</dc:date>
    </item>
    <item>
      <title>Re: Loop, Conditional and Incrementation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-Conditional-and-Incrementation/m-p/531309#M145400</link>
      <description>&lt;P&gt;Hi Novinisrin and Ballardw !&lt;/P&gt;&lt;P&gt;Thanks for your time and help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tested your scripts and they work fine in sandbox but not on live for a reason I can't find.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The closest result was with ballardw script, in witch the result was working fine but only on individuals that started with a "true" YN variable. It would drop the individuals starting with a "false" statement, except the very first individual SUBJID="3".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The script I executed and the output&amp;nbsp;:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1998  data work.want;
1999     set work.have;
2000     by notsorted SUBJID notsorted CMYN;
2001     retain flag;
2002     if first.name then flag=(CMYN='false');
2003     else do;
2004        if flag and (CMYN='false') then delete;
2005        else flag = (CMYN='false');
2006     end;
2007  run;

NOTE: Variable first.name non initialisée.
NOTE: There were 28 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 5 observations and 4 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It's weird because the script is working fine on sandbox data, whatever combinaison I try.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again for your time !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;EDIT : I just realised first. function has "name" as parameter.. that was the problem.&lt;/P&gt;&lt;P&gt;Your scripts are working fine&amp;nbsp;!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm still learning haha.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help !&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jan 2019 12:35:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-Conditional-and-Incrementation/m-p/531309#M145400</guid>
      <dc:creator>Althaea</dc:creator>
      <dc:date>2019-01-30T12:35:29Z</dc:date>
    </item>
  </channel>
</rss>

