<?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 Reference an observation in sas dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Reference-an-observation-in-sas-dataset/m-p/469407#M285509</link>
    <description>&lt;P&gt;All,&amp;nbsp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;I am new to SAS Programming and am trying to learn as I practice. I have what could be a novice's question - how do I reference a specific observation of a SAS data set ? For e.g.:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data _null_; 
	If 0 Then Set Sashelp.class nobs= n; 
	Call Symputx('TotObs',n); 
	Stop; 
Run; 

Data Test; 
 set Sashelp.class;
 Do i = 1 To TotObs; 
 	If i = 1 Then Test = height ; 
 	Else = height[i-1] - 1; /* How to reference the previous observation of the Height column ? */ 
 End;
Run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; Any help is appreciated. Please provide solutions that do not use Proc IML as I don't have it.&lt;/P&gt;</description>
    <pubDate>Mon, 11 Jun 2018 21:10:50 GMT</pubDate>
    <dc:creator>UdayGuntupalli</dc:creator>
    <dc:date>2018-06-11T21:10:50Z</dc:date>
    <item>
      <title>Reference an observation in sas dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reference-an-observation-in-sas-dataset/m-p/469407#M285509</link>
      <description>&lt;P&gt;All,&amp;nbsp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;I am new to SAS Programming and am trying to learn as I practice. I have what could be a novice's question - how do I reference a specific observation of a SAS data set ? For e.g.:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data _null_; 
	If 0 Then Set Sashelp.class nobs= n; 
	Call Symputx('TotObs',n); 
	Stop; 
Run; 

Data Test; 
 set Sashelp.class;
 Do i = 1 To TotObs; 
 	If i = 1 Then Test = height ; 
 	Else = height[i-1] - 1; /* How to reference the previous observation of the Height column ? */ 
 End;
Run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; Any help is appreciated. Please provide solutions that do not use Proc IML as I don't have it.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 21:10:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reference-an-observation-in-sas-dataset/m-p/469407#M285509</guid>
      <dc:creator>UdayGuntupalli</dc:creator>
      <dc:date>2018-06-11T21:10:50Z</dc:date>
    </item>
    <item>
      <title>Re: Reference an observation in sas dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reference-an-observation-in-sas-dataset/m-p/469410#M285510</link>
      <description>&lt;P&gt;In the majority of cases (definitely not all cases), you don't measure across observations.&amp;nbsp; Instead,&amp;nbsp;you create a new variable and retain its value moving forward.&amp;nbsp; For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;/P&gt;
&lt;P&gt;set sashelp.class;&lt;/P&gt;
&lt;P&gt;if _n_=1 then test = height;&lt;/P&gt;
&lt;P&gt;else test = test - 1;&lt;/P&gt;
&lt;P&gt;retain test;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One thing you need to get used to is that SAS doesn't process all the observations at once.&amp;nbsp; It processes just one observation at a time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Additional related tools:&amp;nbsp; the BY statement&amp;nbsp;in a DATA step (first. and last. variables), and the LAG function.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 21:22:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reference-an-observation-in-sas-dataset/m-p/469410#M285510</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-06-11T21:22:20Z</dc:date>
    </item>
    <item>
      <title>Re: Reference an observation in sas dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reference-an-observation-in-sas-dataset/m-p/469413#M285511</link>
      <description>&lt;P&gt;You're using IML type terminology, referencing specific cells and rows.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want the previous value, the LAG and DIF function are also useful, but they're queue functions so make sure you understand how they work.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;RETAIN will hold a value across rows if needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have three major issues with your code that I can see:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A SAS data step loops through the data set automatically so you don't have to loop it yourself. I suspect your loop is entirely unnecessary, as well as the previous step to figure out the number of observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your ELSE statement also is incorrect because it's ELSE= which would create a variable called ELSE that holds some value.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To reference the first row you can use _n_ which is a step counter (not a row counter) so be careful with it. But its good for identifying the first record in a data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to understand the basics of a data step or basic SAS processing I highly recommend the Programming 1 e-course which is freely available.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is probably what you're looking for:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Test; 
 set Sashelp.class;

        x=lag(height);
 	If _n_ = 1 Then Test = height ; 
        else test = x - 1;

       
Run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 21:32:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reference-an-observation-in-sas-dataset/m-p/469413#M285511</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-06-11T21:32:03Z</dc:date>
    </item>
  </channel>
</rss>

