<?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: Loading All the First Observations Using FIRST or LAG1? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loading-All-the-First-Observations-Using-FIRST-or-LAG1/m-p/547682#M151794</link>
    <description>&lt;P&gt;Did you try:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where T=IT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 01 Apr 2019 18:14:49 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-04-01T18:14:49Z</dc:date>
    <item>
      <title>Loading All the First Observations Using FIRST or LAG1?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loading-All-the-First-Observations-Using-FIRST-or-LAG1/m-p/547680#M151793</link>
      <description>&lt;P&gt;Suppose an unbalanced panel data set as follows.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data RAW(drop=IT);
call streaminit(1);
do I=1 to 500;
IT=rand("poisson",5);
do T=IT to IT+rand("poisson",5);
X=rand("normal");
output;
end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;There are 500 Is with random but continuous Ts, then one may subset it as follows.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data SUBSET;
set RAW;
by I T;
if first.I;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Or indirectly without FIRST,&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data SUBSET;
set RAW;
by I T;
if I^=lag1(I);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Both use IF rather than WHERE to subset. Can I do this with WHERE rather than IF? I tried the following but unsuccessful. Thanks.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data SUBSET;
set RAW(where=(first.I=1));
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;(failed as FIRST works only after SET and BY)&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data SUBSET;
set RAW(where=(I^=lag1(I)));
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;(failed as LAGs are invalid in WHERE)&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=SUBSET(where=(I^=lag1(I)));
var X;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;(failed similarly)&lt;/P&gt;</description>
      <pubDate>Mon, 01 Apr 2019 18:11:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loading-All-the-First-Observations-Using-FIRST-or-LAG1/m-p/547680#M151793</guid>
      <dc:creator>Junyong</dc:creator>
      <dc:date>2019-04-01T18:11:39Z</dc:date>
    </item>
    <item>
      <title>Re: Loading All the First Observations Using FIRST or LAG1?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loading-All-the-First-Observations-Using-FIRST-or-LAG1/m-p/547682#M151794</link>
      <description>&lt;P&gt;Did you try:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where T=IT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Apr 2019 18:14:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loading-All-the-First-Observations-Using-FIRST-or-LAG1/m-p/547682#M151794</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-04-01T18:14:49Z</dc:date>
    </item>
    <item>
      <title>Re: Loading All the First Observations Using FIRST or LAG1?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loading-All-the-First-Observations-Using-FIRST-or-LAG1/m-p/547683#M151795</link>
      <description>The code is just a working example I made, and I used IT to make the panel data set unbalanced. There is no IT in the real data sets.</description>
      <pubDate>Mon, 01 Apr 2019 18:18:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loading-All-the-First-Observations-Using-FIRST-or-LAG1/m-p/547683#M151795</guid>
      <dc:creator>Junyong</dc:creator>
      <dc:date>2019-04-01T18:18:53Z</dc:date>
    </item>
    <item>
      <title>Re: Loading All the First Observations Using FIRST or LAG1?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loading-All-the-First-Observations-Using-FIRST-or-LAG1/m-p/547689#M151796</link>
      <description>&lt;P&gt;Then the answer is no. Use the whatever IF statement that works.&lt;/P&gt;
&lt;P&gt;Make a view if it bothers you that much.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data first / view=first;
  set have;
  by id;
&amp;nbsp;&amp;nbsp;if&amp;nbsp;first.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Apr 2019 18:29:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loading-All-the-First-Observations-Using-FIRST-or-LAG1/m-p/547689#M151796</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-04-01T18:29:21Z</dc:date>
    </item>
    <item>
      <title>Re: Loading All the First Observations Using FIRST or LAG1?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loading-All-the-First-Observations-Using-FIRST-or-LAG1/m-p/547710#M151800</link>
      <description>You can't use LAG in WHERE because WHERE follows the SQL limitations and doesn't have access to the other rows. IF is a data set construct instead.</description>
      <pubDate>Mon, 01 Apr 2019 19:34:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loading-All-the-First-Observations-Using-FIRST-or-LAG1/m-p/547710#M151800</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-04-01T19:34:46Z</dc:date>
    </item>
    <item>
      <title>Re: Loading All the First Observations Using FIRST or LAG1?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loading-All-the-First-Observations-Using-FIRST-or-LAG1/m-p/547717#M151805</link>
      <description>It seems I need to explicitly create the FIRST variable before any PROC (or any DATA). Thanks.</description>
      <pubDate>Mon, 01 Apr 2019 20:04:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loading-All-the-First-Observations-Using-FIRST-or-LAG1/m-p/547717#M151805</guid>
      <dc:creator>Junyong</dc:creator>
      <dc:date>2019-04-01T20:04:15Z</dc:date>
    </item>
    <item>
      <title>Re: Loading All the First Observations Using FIRST or LAG1?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loading-All-the-First-Observations-Using-FIRST-or-LAG1/m-p/547722#M151809</link>
      <description>So I need to subset (or create the FIRST variable) it in DATA before PROCs or DATAs. Thank you.</description>
      <pubDate>Mon, 01 Apr 2019 20:07:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loading-All-the-First-Observations-Using-FIRST-or-LAG1/m-p/547722#M151809</guid>
      <dc:creator>Junyong</dc:creator>
      <dc:date>2019-04-01T20:07:19Z</dc:date>
    </item>
    <item>
      <title>Re: Loading All the First Observations Using FIRST or LAG1?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loading-All-the-First-Observations-Using-FIRST-or-LAG1/m-p/547735#M151817</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/173881"&gt;@Junyong&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;So I need to subset (or create the FIRST variable) it in DATA before PROCs or DATAs. Thank you.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, you may want to just add a flag via a view though as suggested via&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data viewTEmp / view=viewTemp;

set have;
by myGroups;

if first.group then first_flag=1;else first_flag=0;

run;&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;Then in future queries, just hit the view instead of the main table and you can access the first_flag variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=viewTemp;
where first_flag=1;
run;&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;Unfortunately this does require that your original data is pre sorted. You could probably write a more complicated SQL view that did this as well, if you need to sort first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Apr 2019 20:39:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loading-All-the-First-Observations-Using-FIRST-or-LAG1/m-p/547735#M151817</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-04-01T20:39:36Z</dc:date>
    </item>
  </channel>
</rss>

