<?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 FIRST.var; does not pick observations correctly in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/IF-FIRST-var-does-not-pick-observations-correctly/m-p/607043#M17496</link>
    <description>&lt;P&gt;Thank you all for your replies.&lt;/P&gt;</description>
    <pubDate>Mon, 25 Nov 2019 16:25:05 GMT</pubDate>
    <dc:creator>myao</dc:creator>
    <dc:date>2019-11-25T16:25:05Z</dc:date>
    <item>
      <title>IF FIRST.var; does not pick observations correctly</title>
      <link>https://communities.sas.com/t5/New-SAS-User/IF-FIRST-var-does-not-pick-observations-correctly/m-p/605915#M17310</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a script as follows.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.tst001;
	input x_ch $ y_ch $ z;

	datalines;
a A 1
a A 2
a B 1
a B 2
b A 1
b B 1
b B 2
;
run;

proc sort data=work.tst001; by x_ch y_ch z; run;

data work.tst004;
	set work.tst001;
	by x_ch y_ch z; 

	if first.z;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;tst004 should have 6 observations. But the code above chooses all observations in tst001. Why?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;MYao&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2019 19:59:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/IF-FIRST-var-does-not-pick-observations-correctly/m-p/605915#M17310</guid>
      <dc:creator>myao</dc:creator>
      <dc:date>2019-11-20T19:59:03Z</dc:date>
    </item>
    <item>
      <title>Re: IF FIRST.var; does not pick observations correctly</title>
      <link>https://communities.sas.com/t5/New-SAS-User/IF-FIRST-var-does-not-pick-observations-correctly/m-p/605916#M17311</link>
      <description>&lt;P&gt;Because first.z evaluates to 1 for each of your records. See the code below.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.tst001;
	input x_ch $ y_ch $ z;

	datalines;
a A 1
a A 2
a B 1
a B 2
b A 1
b B 1
b B 2
;
run;

proc sort data=work.tst001; by x_ch y_ch z; run;

data work.tst004;
	set work.tst001;
	by x_ch y_ch z; 
   first_z=first.z;
	if first.z;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Nov 2019 20:01:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/IF-FIRST-var-does-not-pick-observations-correctly/m-p/605916#M17311</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-11-20T20:01:40Z</dc:date>
    </item>
    <item>
      <title>Re: IF FIRST.var; does not pick observations correctly</title>
      <link>https://communities.sas.com/t5/New-SAS-User/IF-FIRST-var-does-not-pick-observations-correctly/m-p/605918#M17312</link>
      <description>&lt;P&gt;Which observations are you expecting it to exclude?&lt;/P&gt;
&lt;PRE&gt;497   data _null_;
498     set tst001;
499     by x_ch y_ch z ;
500     put (_all_ first.x_ch first.y_ch first.z) (=);
501   run;

x_ch=a y_ch=A z=1 FIRST.x_ch=1 FIRST.y_ch=1 FIRST.z=1
x_ch=a y_ch=A z=2 FIRST.x_ch=0 FIRST.y_ch=0 FIRST.z=1
x_ch=a y_ch=B z=1 FIRST.x_ch=0 FIRST.y_ch=1 FIRST.z=1
x_ch=a y_ch=B z=2 FIRST.x_ch=0 FIRST.y_ch=0 FIRST.z=1
x_ch=b y_ch=A z=1 FIRST.x_ch=1 FIRST.y_ch=1 FIRST.z=1
x_ch=b y_ch=B z=1 FIRST.x_ch=0 FIRST.y_ch=1 FIRST.z=1
x_ch=b y_ch=B z=2 FIRST.x_ch=0 FIRST.y_ch=0 FIRST.z=1
&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Nov 2019 20:04:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/IF-FIRST-var-does-not-pick-observations-correctly/m-p/605918#M17312</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-11-20T20:04:44Z</dc:date>
    </item>
    <item>
      <title>Re: IF FIRST.var; does not pick observations correctly</title>
      <link>https://communities.sas.com/t5/New-SAS-User/IF-FIRST-var-does-not-pick-observations-correctly/m-p/605919#M17313</link>
      <description>&lt;P&gt;FIRST.Z will pick up the first obs from the group defined by x_ch, y_ch &lt;U&gt;and&lt;/U&gt; z values. I think you should use first.y_ch. It will pick the first obs from the group defined by x_ch and y_ch.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2019 20:06:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/IF-FIRST-var-does-not-pick-observations-correctly/m-p/605919#M17313</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-11-20T20:06:49Z</dc:date>
    </item>
    <item>
      <title>Re: IF FIRST.var; does not pick observations correctly</title>
      <link>https://communities.sas.com/t5/New-SAS-User/IF-FIRST-var-does-not-pick-observations-correctly/m-p/605920#M17314</link>
      <description>&lt;P&gt;When several variables appear in a by statement, they constitute a hierarchy from left to right. A group change further left forces a change in all levels to the right. E.g.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;by a b c;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A change in b will automatically force a change in c, a change in a will force a change in b and c.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2019 20:07:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/IF-FIRST-var-does-not-pick-observations-correctly/m-p/605920#M17314</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-11-20T20:07:24Z</dc:date>
    </item>
    <item>
      <title>Re: IF FIRST.var; does not pick observations correctly</title>
      <link>https://communities.sas.com/t5/New-SAS-User/IF-FIRST-var-does-not-pick-observations-correctly/m-p/605921#M17315</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/300527"&gt;@myao&lt;/a&gt;&amp;nbsp;my guess is that this is what you want to do?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.tst004;
	set work.tst001;
	by z notsorted;
	if first.z;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The result is the six obs&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;x_ch y_ch z 
a    A    1 
a    A    2 
a    B    1 
a    B    2 
b    A    1 
b    B    2 &lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Nov 2019 20:07:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/IF-FIRST-var-does-not-pick-observations-correctly/m-p/605921#M17315</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-11-20T20:07:30Z</dc:date>
    </item>
    <item>
      <title>Re: IF FIRST.var; does not pick observations correctly</title>
      <link>https://communities.sas.com/t5/New-SAS-User/IF-FIRST-var-does-not-pick-observations-correctly/m-p/605939#M17316</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/300527"&gt;@myao&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a script as follows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;tst004 should have 6 observations. But the code above chooses all observations in tst001. Why?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;MYao&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;As others have asked:&amp;nbsp; &lt;STRONG&gt;which 6 do you expect in the output?&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2019 20:36:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/IF-FIRST-var-does-not-pick-observations-correctly/m-p/605939#M17316</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-11-20T20:36:43Z</dc:date>
    </item>
    <item>
      <title>Re: IF FIRST.var; does not pick observations correctly</title>
      <link>https://communities.sas.com/t5/New-SAS-User/IF-FIRST-var-does-not-pick-observations-correctly/m-p/607039#M17493</link>
      <description>&lt;P&gt;Thank you all the your replies.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 16:23:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/IF-FIRST-var-does-not-pick-observations-correctly/m-p/607039#M17493</guid>
      <dc:creator>myao</dc:creator>
      <dc:date>2019-11-25T16:23:38Z</dc:date>
    </item>
    <item>
      <title>Re: IF FIRST.var; does not pick observations correctly</title>
      <link>https://communities.sas.com/t5/New-SAS-User/IF-FIRST-var-does-not-pick-observations-correctly/m-p/607043#M17496</link>
      <description>&lt;P&gt;Thank you all for your replies.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 16:25:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/IF-FIRST-var-does-not-pick-observations-correctly/m-p/607043#M17496</guid>
      <dc:creator>myao</dc:creator>
      <dc:date>2019-11-25T16:25:05Z</dc:date>
    </item>
  </channel>
</rss>

