<?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: If condition with By group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/If-condition-with-By-group/m-p/554382#M154213</link>
    <description>Thank you, that would work, but I still quite confused why first.interval&lt;BR /&gt;wouldn't work here. Is that a group that is nested inside the date?&lt;BR /&gt;</description>
    <pubDate>Fri, 26 Apr 2019 19:34:19 GMT</pubDate>
    <dc:creator>Xusheng</dc:creator>
    <dc:date>2019-04-26T19:34:19Z</dc:date>
    <item>
      <title>If condition with By group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-condition-with-By-group/m-p/554378#M154209</link>
      <description>&lt;P&gt;Hi, I was trying to make the first return of each day equals to 0 and keep the rest returns unchanged. 'Return' is the variable that I calculated based on price.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the sample of Have:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Interval Return date ：ddmmyy10. ;
datalines;
1 23	03/09/2012
2 32	03/09/2012
1 45	04/09/2012
2 12	04/09/2012
3 56	04/09/2012
1 43	05/09/2012
1 76	06/09/2012
2 43	06/09/2012
3 34	06/09/2012
4 54	06/09/2012
1 39	07/09/2012
1 54	08/09/2012
2 43	08/09/2012
1 65	10/09/2012


run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Want:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Want;
input Interval Return date ：ddmmyy10. ;
datalines;
1 0	03/09/2012
2 32	03/09/2012
1 0	04/09/2012
2 12	04/09/2012
3 56	04/09/2012
1 0	05/09/2012
1 0	06/09/2012
2 43	06/09/2012
3 34	06/09/2012
4 54	06/09/2012
1 0	07/09/2012
1 0	08/09/2012
2 43	08/09/2012
1 0	10/09/2012


run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here is the code I tried:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc sort data=Have;
	By Date Interval;
Run;

Data Want;
	Set Have;	
		By Date Interval;
		if first.Date or  first.Interval then Return = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I got all the returns turned to 0. Is there any suggestion? Thank you in advance.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2019 19:28:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-condition-with-By-group/m-p/554378#M154209</guid>
      <dc:creator>Xusheng</dc:creator>
      <dc:date>2019-04-26T19:28:17Z</dc:date>
    </item>
    <item>
      <title>Re: If condition with By group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-condition-with-By-group/m-p/554379#M154210</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Want;
	Set Have;	
		By Date Interval;
		if first.Date then Return = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Apr 2019 19:29:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-condition-with-By-group/m-p/554379#M154210</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-04-26T19:29:50Z</dc:date>
    </item>
    <item>
      <title>Re: If condition with By group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-condition-with-By-group/m-p/554380#M154211</link>
      <description>&lt;PRE&gt;Data Want;
	Set Have;	
		By Date ;
		if first.Date  then Return = 0;
run;&lt;/PRE&gt;
&lt;P&gt;At least in your example data there were no duplicates of interval within a date so every record was true for first.interval.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2019 19:32:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-condition-with-By-group/m-p/554380#M154211</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-04-26T19:32:39Z</dc:date>
    </item>
    <item>
      <title>Re: If condition with By group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-condition-with-By-group/m-p/554381#M154212</link>
      <description>Thank you, this makes more sense.&lt;BR /&gt;</description>
      <pubDate>Fri, 26 Apr 2019 19:34:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-condition-with-By-group/m-p/554381#M154212</guid>
      <dc:creator>Xusheng</dc:creator>
      <dc:date>2019-04-26T19:34:17Z</dc:date>
    </item>
    <item>
      <title>Re: If condition with By group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-condition-with-By-group/m-p/554382#M154213</link>
      <description>Thank you, that would work, but I still quite confused why first.interval&lt;BR /&gt;wouldn't work here. Is that a group that is nested inside the date?&lt;BR /&gt;</description>
      <pubDate>Fri, 26 Apr 2019 19:34:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-condition-with-By-group/m-p/554382#M154213</guid>
      <dc:creator>Xusheng</dc:creator>
      <dc:date>2019-04-26T19:34:19Z</dc:date>
    </item>
    <item>
      <title>Re: If condition with By group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-condition-with-By-group/m-p/554383#M154214</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/114336"&gt;@Xusheng&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you, that would work, but I still quite confused why first.interval&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Here is the explanation&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You asked for "&lt;SPAN&gt;I was trying to make the first return of each day equals to 0" which in SAS code translates to&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.date then return=0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;First.interval does NOT produce the stated requirement of "&lt;SPAN&gt;I was trying to make the first return of each day equals to 0"&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Apr 2019 10:39:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-condition-with-By-group/m-p/554383#M154214</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-04-28T10:39:43Z</dc:date>
    </item>
    <item>
      <title>Re: If condition with By group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-condition-with-By-group/m-p/554419#M154222</link>
      <description>&lt;P&gt;When you use multiple variables in a BY statement then yes they are "nested".&amp;nbsp; So the first variable divides the data into groups. The second variable in the BY list is then used to divide those groups up into even smaller groups.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if you use&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;by date interval;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then FIRST.INTERVAL will be true when the value of INTERVAL changes WITHIN the current value of DATE.&lt;/P&gt;
&lt;P&gt;So if FIRST.DATE is true then FIRST.INTERVAL has to also be true because if it is the first observation for that value of DATE then it must also by definition be the first observation for that value of INTERVAL within that value of DATE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a simple example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;date interval first.date last.date first.interval last.interval
2018/01/01 A 1 0 1 0
2018/01/01 A 0 0 0 1
2018/01/01 B 0 0 1 1
2018/01/01 C 0 1 1 1
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2019 23:04:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-condition-with-By-group/m-p/554419#M154222</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-04-26T23:04:10Z</dc:date>
    </item>
  </channel>
</rss>

