<?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: Set to missing after a specific value in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Set-to-missing-after-a-specific-value/m-p/631440#M18931</link>
    <description>&lt;P&gt;Essentially, once flag is set&amp;nbsp;&lt;STRONG&gt;if flag&lt;/STRONG&gt;&amp;nbsp;&lt;STRONG&gt;then y=.;&amp;nbsp;&lt;/STRONG&gt;clause will always happen -- flag will never change value until the next first.id=1 occurs&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's a matter of figuring out a 'state machine' that produces the values relevant to your processing needs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;At first.ID the flag is reset to 0&lt;/LI&gt;
&lt;LI&gt;Then, because the flag is 0 the &lt;STRONG&gt;else&lt;/STRONG&gt; clause is executed, and the flag is set to the y value( flag=y).
&lt;UL&gt;
&lt;LI&gt;If y=0 the flag remains 0&lt;/LI&gt;
&lt;LI&gt;if y=1 the flag is set to 1&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;LI&gt;Control returns to the top of the step and the next row is read into the PDV&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Let's unroll that return to top.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;flag is retained, so it's value will persist across rows, and will only change when it is assigned a new value&lt;/LI&gt;
&lt;LI&gt;At second observation (and subsequent obs), presuming we are in same ID group:
&lt;UL&gt;
&lt;LI&gt;the flag is&amp;nbsp;&lt;STRONG&gt;NOT&lt;/STRONG&gt; reset in the &lt;STRONG&gt;if first.id&lt;/STRONG&gt; because by processing will cause first.ID to be 0&lt;/LI&gt;
&lt;LI&gt;if the prior row had y=0, flag will be 0 and the&amp;nbsp;&lt;STRONG&gt;else&amp;nbsp;&lt;/STRONG&gt;clause is executed again and the flag is set to the current y value&lt;/LI&gt;
&lt;LI&gt;if the prior row had y=1, flag will be 1 and the&amp;nbsp;&lt;STRONG&gt;then&amp;nbsp;&lt;/STRONG&gt;clause is executed, setting y to missing.
&lt;UL&gt;
&lt;LI&gt;Because flag is 1&amp;nbsp;&lt;STRONG&gt;if flag&lt;/STRONG&gt; will always be true, and thus&lt;/LI&gt;
&lt;LI&gt;flag will not change until the next first.id, so all future rows of group will have flag = 1 resulting in a y=. assignment.&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;LI&gt;&amp;nbsp;&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;/UL&gt;</description>
    <pubDate>Thu, 12 Mar 2020 03:47:59 GMT</pubDate>
    <dc:creator>RichardDeVen</dc:creator>
    <dc:date>2020-03-12T03:47:59Z</dc:date>
    <item>
      <title>Set to missing after a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Set-to-missing-after-a-specific-value/m-p/631419#M18927</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create a dataset that looks like data "want" below, for id 1-100. This is for an exercise so the values don't matter, but the data structure needs to follow these rules:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;6 rows (time 0-5) for every id.&lt;/LI&gt;&lt;LI&gt;For each id, once y=1, then next rows will be missing.&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;For each id, y=0 for rows before y=1.&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;20% of the ids have y=1.&amp;nbsp;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;I have tried a few approaches, but I am running into issues. For example, this attempt goes from wide to long. Within each id, how do I set values after 1 to missing?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sim1; 
	do id=1 to 100;
	year0=0; 
	year1=1;
	year2=2;
	year3=3;
	year4=4;
	year5=5;
	output; end;
run;

proc transpose data=sim1 out=sim2; 
	by id;  
run;

data sim3; 
	set sim2 (rename=(COL1=year)); 
		y=rand("BINOMIAL", 0.2, 1);
	drop _name_;
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want; 
	input id y time; 
	datalines; 
		1 1 0
		1 . 1
		1 . 2
		1 . 3
		1 . 4
		1 . 5
		2 0 0
		2 0 1
		2 0 2
		2 1 3
		2 . 4
		2 . 5
		;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Mar 2020 23:54:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Set-to-missing-after-a-specific-value/m-p/631419#M18927</guid>
      <dc:creator>pamplemousse822</dc:creator>
      <dc:date>2020-03-11T23:54:10Z</dc:date>
    </item>
    <item>
      <title>Re: Set to missing after a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Set-to-missing-after-a-specific-value/m-p/631424#M18928</link>
      <description>&lt;P&gt;In a DATA Step use a retained flag variable to track the occurrence of y=1 in the ID group.&lt;/P&gt;
&lt;P&gt;Set the y value to missing when the flag is active.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this example, because y is 0 or 1, the flag can be assigned to the y value (up to the point at which y=1).&amp;nbsp; After flag is assigned 1 the else never occurs again (within the BY group.)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set sim3; 
  by id;

  if first.id then flag = 0;

  if flag then 
    y  = .;
  else
    flag = y;

  retain flag; drop flag;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Mar 2020 00:54:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Set-to-missing-after-a-specific-value/m-p/631424#M18928</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-03-12T00:54:00Z</dc:date>
    </item>
    <item>
      <title>Re: Set to missing after a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Set-to-missing-after-a-specific-value/m-p/631426#M18930</link>
      <description>This worked! Thank you!&lt;BR /&gt;&lt;BR /&gt;Can you explain how the flag knows to stop at 1?</description>
      <pubDate>Thu, 12 Mar 2020 01:02:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Set-to-missing-after-a-specific-value/m-p/631426#M18930</guid>
      <dc:creator>pamplemousse822</dc:creator>
      <dc:date>2020-03-12T01:02:50Z</dc:date>
    </item>
    <item>
      <title>Re: Set to missing after a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Set-to-missing-after-a-specific-value/m-p/631440#M18931</link>
      <description>&lt;P&gt;Essentially, once flag is set&amp;nbsp;&lt;STRONG&gt;if flag&lt;/STRONG&gt;&amp;nbsp;&lt;STRONG&gt;then y=.;&amp;nbsp;&lt;/STRONG&gt;clause will always happen -- flag will never change value until the next first.id=1 occurs&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's a matter of figuring out a 'state machine' that produces the values relevant to your processing needs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;At first.ID the flag is reset to 0&lt;/LI&gt;
&lt;LI&gt;Then, because the flag is 0 the &lt;STRONG&gt;else&lt;/STRONG&gt; clause is executed, and the flag is set to the y value( flag=y).
&lt;UL&gt;
&lt;LI&gt;If y=0 the flag remains 0&lt;/LI&gt;
&lt;LI&gt;if y=1 the flag is set to 1&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;LI&gt;Control returns to the top of the step and the next row is read into the PDV&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Let's unroll that return to top.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;flag is retained, so it's value will persist across rows, and will only change when it is assigned a new value&lt;/LI&gt;
&lt;LI&gt;At second observation (and subsequent obs), presuming we are in same ID group:
&lt;UL&gt;
&lt;LI&gt;the flag is&amp;nbsp;&lt;STRONG&gt;NOT&lt;/STRONG&gt; reset in the &lt;STRONG&gt;if first.id&lt;/STRONG&gt; because by processing will cause first.ID to be 0&lt;/LI&gt;
&lt;LI&gt;if the prior row had y=0, flag will be 0 and the&amp;nbsp;&lt;STRONG&gt;else&amp;nbsp;&lt;/STRONG&gt;clause is executed again and the flag is set to the current y value&lt;/LI&gt;
&lt;LI&gt;if the prior row had y=1, flag will be 1 and the&amp;nbsp;&lt;STRONG&gt;then&amp;nbsp;&lt;/STRONG&gt;clause is executed, setting y to missing.
&lt;UL&gt;
&lt;LI&gt;Because flag is 1&amp;nbsp;&lt;STRONG&gt;if flag&lt;/STRONG&gt; will always be true, and thus&lt;/LI&gt;
&lt;LI&gt;flag will not change until the next first.id, so all future rows of group will have flag = 1 resulting in a y=. assignment.&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;LI&gt;&amp;nbsp;&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Thu, 12 Mar 2020 03:47:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Set-to-missing-after-a-specific-value/m-p/631440#M18931</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-03-12T03:47:59Z</dc:date>
    </item>
  </channel>
</rss>

