<?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 Compare variable against first record in dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Compare-variable-against-first-record-in-dataset/m-p/403731#M98106</link>
    <description>&lt;P&gt;Hi everyone -&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset where I'd like to compare a record against the first row in the dataset. For example, if I have the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; Height&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 40&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 35&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; 40&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; 58&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to be able to create another variable that says which ones are greater than the first.&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; Height&amp;nbsp; &amp;nbsp; Flag&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 40&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 35&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; 40&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; 58&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't think a lag will work since I always want to compare against record 1, not the record immediately preceding. And I can't get a first. command to work since I need to sort by ID in order to get my comparison row on the top, though I think there may be a way to do this that I can't figure out.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas? Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 12 Oct 2017 23:41:35 GMT</pubDate>
    <dc:creator>ATLien</dc:creator>
    <dc:date>2017-10-12T23:41:35Z</dc:date>
    <item>
      <title>Compare variable against first record in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-variable-against-first-record-in-dataset/m-p/403731#M98106</link>
      <description>&lt;P&gt;Hi everyone -&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset where I'd like to compare a record against the first row in the dataset. For example, if I have the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; Height&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 40&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 35&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; 40&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; 58&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to be able to create another variable that says which ones are greater than the first.&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; Height&amp;nbsp; &amp;nbsp; Flag&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 40&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 35&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; 40&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; 58&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't think a lag will work since I always want to compare against record 1, not the record immediately preceding. And I can't get a first. command to work since I need to sort by ID in order to get my comparison row on the top, though I think there may be a way to do this that I can't figure out.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas? Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Oct 2017 23:41:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-variable-against-first-record-in-dataset/m-p/403731#M98106</guid>
      <dc:creator>ATLien</dc:creator>
      <dc:date>2017-10-12T23:41:35Z</dc:date>
    </item>
    <item>
      <title>Re: Compare variable against first record in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-variable-against-first-record-in-dataset/m-p/403732#M98107</link>
      <description>&lt;P&gt;RETAIN.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;RETAIN first_record_value;
if _n_=1 /*check for first record*/
     then first_record_value = height;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/171381"&gt;@ATLien&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi everyone -&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset where I'd like to compare a record against the first row in the dataset. For example, if I have the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp; Height&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 40&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 35&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; 40&lt;/P&gt;
&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; 58&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to be able to create another variable that says which ones are greater than the first.&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp; Height&amp;nbsp; &amp;nbsp; Flag&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 40&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 35&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; 40&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; 58&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't think a lag will work since I always want to compare against record 1, not the record immediately preceding. And I can't get a first. command to work since I need to sort by ID in order to get my comparison row on the top, though I think there may be a way to do this that I can't figure out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any ideas? Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Oct 2017 23:51:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-variable-against-first-record-in-dataset/m-p/403732#M98107</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-10-12T23:51:37Z</dc:date>
    </item>
    <item>
      <title>Re: Compare variable against first record in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-variable-against-first-record-in-dataset/m-p/403736#M98110</link>
      <description>&lt;P&gt;When I try that, it just adds in a new variable with a record only for the first row. I still only have the relevant data in that top row, which I want to compare against data in subsequent rows.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 00:04:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-variable-against-first-record-in-dataset/m-p/403736#M98110</guid>
      <dc:creator>ATLien</dc:creator>
      <dc:date>2017-10-13T00:04:41Z</dc:date>
    </item>
    <item>
      <title>Re: Compare variable against first record in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-variable-against-first-record-in-dataset/m-p/403738#M98112</link>
      <description>&lt;P&gt;Did you include the RETAIN statement?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Post your exact code please.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 00:24:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-variable-against-first-record-in-dataset/m-p/403738#M98112</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-10-13T00:24:29Z</dc:date>
    </item>
    <item>
      <title>Re: Compare variable against first record in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-variable-against-first-record-in-dataset/m-p/403739#M98113</link>
      <description>&lt;P&gt;Use two SET statements.&amp;nbsp; One SET read just the single first obs.&amp;nbsp; The other reads the entire data set, and does the compare:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id height;
datalines;
1      40
2      35
3      40
4      58
run;
data want (drop=h_ref);
  if _n_=1 then set have (keep=height rename=(height=h_ref));
  set have;
  flag=height&amp;gt;h_ref;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "&lt;EM&gt;&lt;STRONG&gt;if _n_=1 then set have ..."&lt;/STRONG&gt;&lt;/EM&gt; reads observation 1, renaming height to h_ref.&amp;nbsp; Just as importantly any variable read by SET&amp;nbsp; is automatically "retained" until that same SET statement is executed in a later iteration of the data set.&amp;nbsp; But because the program does not re-execute that set, variable H_REF is retained throughout all the observations.&amp;nbsp; Meanwhile the other SET statement reads all the obs, from obs 1 to the end.&amp;nbsp; I.e., the two SET statements set up separate streams of data from HAVE.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 00:31:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-variable-against-first-record-in-dataset/m-p/403739#M98113</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-10-13T00:31:35Z</dc:date>
    </item>
    <item>
      <title>Re: Compare variable against first record in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-variable-against-first-record-in-dataset/m-p/403741#M98114</link>
      <description>&lt;P&gt;Thank you! I had never thought to use two set statements but this works perfectly. What a great new trick!&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 00:37:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-variable-against-first-record-in-dataset/m-p/403741#M98114</guid>
      <dc:creator>ATLien</dc:creator>
      <dc:date>2017-10-13T00:37:49Z</dc:date>
    </item>
  </channel>
</rss>

