<?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 Set variable value = TRUE/FALSE for all occurrences, once the first one is realized? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Set-variable-value-TRUE-FALSE-for-all-occurrences-once-the-first/m-p/644816#M192668</link>
    <description>&lt;P&gt;If I have my data sorted by ID and date,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile DATALINES dsd missover;
input ID date base_var;
CARDS;
01, 1, 0
01, 2, 0
01, 3, 1
01, 4, 0
01, 5, 0
01, 6, 0
02, 1, 1
02, 2, 1
02, 3, 0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and my desired output is as follows:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
infile DATALINES dsd missover;
input ID date base_var first_condition second_condition;
CARDS;
01, 1, 0, 0, 1
01, 2, 0, 0, 1
01, 3, 1, 0, 0
01, 4, 0, 1, 0
01, 5, 0, 1, 0
01, 6, 0, 1, 0
02, 1, 1, 0, 0
02, 2, 1, 1, 0
02, 3, 0, 1, 0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So you can see, I have a base variable, and 2 variables I want to condition on the base variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want it so that when base_var = 0, first_condition = 0, and second_condition =1.&lt;/P&gt;&lt;P&gt;But, for the first instance of base_var = 1, second_condition = 0 for&amp;nbsp;the rest of the rows, for that specific ID.&lt;/P&gt;&lt;P&gt;and for the first instance of lag(base_var) =1, first_condition = 1 for the rest of the rows, for that specific ID.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So in this case, at ID = 01, date = 3 - you can see the third row is when this second_condition =0,&lt;/P&gt;&lt;P&gt;and then it resets when I get to ID = 2.&lt;/P&gt;&lt;P&gt;Similarly at ID = 01, date = 4 - first_condition = 1 for the remaining ID=01 rows, until we get to ID=02 where it goes back to 0.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is this possible?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what I tried, and it isn't working:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want;
	SET have;&lt;BR /&gt;    first_condition = 0;
	second_condition = 1;
	if lag(base_var) = 1 THEN first_condition = 1;
    if lag(first_condition ) = 1 THEN first_condition = 1;	
	if ID ne lag(ID) THEN first_condition = 0; *so first_condition=0 every time we have the first obs of a new id;
 
	if base_var= 1 THEN second_condition = 0;
	if first_condition = 1 THEN second_condition = 0;
	if id ne lag(id) and base_var = 1 THEN second_condition= 0; *so second_condition =0 if base_Var = 1 in the first obs of a new id;
run;	&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But when I do this, my code messes up at ID=01, date = 6, when I have two base_var= 0s in a row.&lt;/P&gt;&lt;P&gt;first_condition = 0 and second_condition = 1 in this row,&lt;/P&gt;&lt;P&gt;which I don't get, because I thought the&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;if lag(first_condition ) = 1 THEN first_condition = 1;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;part would take care of it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help appreciated.&lt;/P&gt;</description>
    <pubDate>Sun, 03 May 2020 11:19:13 GMT</pubDate>
    <dc:creator>UniversitySas</dc:creator>
    <dc:date>2020-05-03T11:19:13Z</dc:date>
    <item>
      <title>Set variable value = TRUE/FALSE for all occurrences, once the first one is realized?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-variable-value-TRUE-FALSE-for-all-occurrences-once-the-first/m-p/644816#M192668</link>
      <description>&lt;P&gt;If I have my data sorted by ID and date,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile DATALINES dsd missover;
input ID date base_var;
CARDS;
01, 1, 0
01, 2, 0
01, 3, 1
01, 4, 0
01, 5, 0
01, 6, 0
02, 1, 1
02, 2, 1
02, 3, 0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and my desired output is as follows:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
infile DATALINES dsd missover;
input ID date base_var first_condition second_condition;
CARDS;
01, 1, 0, 0, 1
01, 2, 0, 0, 1
01, 3, 1, 0, 0
01, 4, 0, 1, 0
01, 5, 0, 1, 0
01, 6, 0, 1, 0
02, 1, 1, 0, 0
02, 2, 1, 1, 0
02, 3, 0, 1, 0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So you can see, I have a base variable, and 2 variables I want to condition on the base variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want it so that when base_var = 0, first_condition = 0, and second_condition =1.&lt;/P&gt;&lt;P&gt;But, for the first instance of base_var = 1, second_condition = 0 for&amp;nbsp;the rest of the rows, for that specific ID.&lt;/P&gt;&lt;P&gt;and for the first instance of lag(base_var) =1, first_condition = 1 for the rest of the rows, for that specific ID.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So in this case, at ID = 01, date = 3 - you can see the third row is when this second_condition =0,&lt;/P&gt;&lt;P&gt;and then it resets when I get to ID = 2.&lt;/P&gt;&lt;P&gt;Similarly at ID = 01, date = 4 - first_condition = 1 for the remaining ID=01 rows, until we get to ID=02 where it goes back to 0.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is this possible?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what I tried, and it isn't working:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want;
	SET have;&lt;BR /&gt;    first_condition = 0;
	second_condition = 1;
	if lag(base_var) = 1 THEN first_condition = 1;
    if lag(first_condition ) = 1 THEN first_condition = 1;	
	if ID ne lag(ID) THEN first_condition = 0; *so first_condition=0 every time we have the first obs of a new id;
 
	if base_var= 1 THEN second_condition = 0;
	if first_condition = 1 THEN second_condition = 0;
	if id ne lag(id) and base_var = 1 THEN second_condition= 0; *so second_condition =0 if base_Var = 1 in the first obs of a new id;
run;	&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But when I do this, my code messes up at ID=01, date = 6, when I have two base_var= 0s in a row.&lt;/P&gt;&lt;P&gt;first_condition = 0 and second_condition = 1 in this row,&lt;/P&gt;&lt;P&gt;which I don't get, because I thought the&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;if lag(first_condition ) = 1 THEN first_condition = 1;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;part would take care of it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help appreciated.&lt;/P&gt;</description>
      <pubDate>Sun, 03 May 2020 11:19:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-variable-value-TRUE-FALSE-for-all-occurrences-once-the-first/m-p/644816#M192668</guid>
      <dc:creator>UniversitySas</dc:creator>
      <dc:date>2020-05-03T11:19:13Z</dc:date>
    </item>
    <item>
      <title>Re: Set variable value = TRUE/FALSE for all occurrences, once the first one is realized?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-variable-value-TRUE-FALSE-for-all-occurrences-once-the-first/m-p/644823#M192672</link>
      <description>&lt;P&gt;Assuming I understood what you mean .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile DATALINES dsd missover;
input ID date base_var;
CARDS;
01, 1, 0
01, 2, 0
01, 3, 1
01, 4, 0
01, 5, 0
01, 6, 0
02, 1, 1
02, 2, 1
02, 3, 0
;
run;
data want;
 set have;
 by id;
 retain first second;
 if first.id then do;first=0;second=1;end;
 if base_var=1 then second=0;
 if id=lag(id) and lag(base_var)=1 then first=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 03 May 2020 11:51:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-variable-value-TRUE-FALSE-for-all-occurrences-once-the-first/m-p/644823#M192672</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-05-03T11:51:53Z</dc:date>
    </item>
    <item>
      <title>Re: Set variable value = TRUE/FALSE for all occurrences, once the first one is realized?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-variable-value-TRUE-FALSE-for-all-occurrences-once-the-first/m-p/644824#M192673</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/95638"&gt;@UniversitySas&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can achieve the desired result without using the (tricky) LAG function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
first_condition=0;
second_condition=1;
do until(last.id);
  set have;
  by id;
  if base_var=1 then second_condition=0;
  output;
  if base_var=1 then first_condition=1;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 03 May 2020 12:04:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-variable-value-TRUE-FALSE-for-all-occurrences-once-the-first/m-p/644824#M192673</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-05-03T12:04:35Z</dc:date>
    </item>
    <item>
      <title>Re: Set variable value = TRUE/FALSE for all occurrences, once the first one is realized?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-variable-value-TRUE-FALSE-for-all-occurrences-once-the-first/m-p/644828#M192677</link>
      <description>THIS WORKED!&lt;BR /&gt;&lt;BR /&gt;thank you so much.&lt;BR /&gt;&lt;BR /&gt;Could I ask - what does the "output" do here? and why is it that first_condition = 1 contingent on the lagged base_var here, and not the current base_var?</description>
      <pubDate>Sun, 03 May 2020 12:20:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-variable-value-TRUE-FALSE-for-all-occurrences-once-the-first/m-p/644828#M192677</guid>
      <dc:creator>UniversitySas</dc:creator>
      <dc:date>2020-05-03T12:20:46Z</dc:date>
    </item>
    <item>
      <title>Re: Set variable value = TRUE/FALSE for all occurrences, once the first one is realized?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-variable-value-TRUE-FALSE-for-all-occurrences-once-the-first/m-p/644835#M192682</link>
      <description>&lt;P&gt;You're welcome. Of course,&amp;nbsp;Ksharp's solution works as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &lt;A href="https://documentation.sas.com/?docsetId=lestmtsref&amp;amp;docsetTarget=n1lltvbis7ye1an1eryo4leh2mck.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank" rel="noopener"&gt;OUTPUT statement&lt;/A&gt; writes the current observation to dataset WANT (and it overrides the implied OUTPUT statement at the end of each DATA step iteration). It is executed unconditionally for every observation of dataset HAVE &lt;EM&gt;after&lt;/EM&gt; &lt;FONT face="courier new,courier"&gt;second_condition&lt;/FONT&gt; was set to 0, if applicable, but &lt;EM&gt;before&lt;/EM&gt; &lt;FONT face="courier new,courier"&gt;first_condition&lt;/FONT&gt; is set to 1, if applicable. Since &lt;FONT face="courier new,courier"&gt;first_condition&lt;/FONT&gt;&amp;nbsp;(and likewise &lt;FONT face="courier new,courier"&gt;second_condition&lt;/FONT&gt;) retains its value within the DO-UNTIL loop which is processing one BY group after the other (known as "DOW loop"), the assignment&amp;nbsp;&lt;FONT face="courier new,courier"&gt;first_condition=1&lt;/FONT&gt; affects the OUTPUT statement of the &lt;EM&gt;next&lt;/EM&gt;&amp;nbsp;and all subsequent iterations of the DO-UNTIL loop within the same BY group, hence all observations in dataset WANT &lt;EM&gt;after&lt;/EM&gt; the one with &lt;FONT face="courier new,courier"&gt;base_var=1&lt;/FONT&gt; until the end of the BY group. The two assignment statements at the beginning of the DATA step initialize the two "condition" variables&amp;nbsp;before the processing of each BY group commences.&lt;/P&gt;</description>
      <pubDate>Sun, 03 May 2020 14:01:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-variable-value-TRUE-FALSE-for-all-occurrences-once-the-first/m-p/644835#M192682</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-05-03T14:01:48Z</dc:date>
    </item>
  </channel>
</rss>

