<?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: Indexing a column + lagged variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Indexing-a-column-lagged-variables/m-p/497276#M131750</link>
    <description>&lt;P&gt;I think a good starting place would be &lt;A href="https://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=n06cy7dznbx6gen1q9mat8de6rdq.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank"&gt;Understanding SAS Indexes&lt;/A&gt;. With an index for a variable X (or a combination of variables) you could (among other things) use the KEY= option of the SET statement in place of the POINT= option in my example, provided that values of X characterize the target observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another powerful and flexible (though complex) tool for non-sequential retrieval and modification of data is the hash object (see&amp;nbsp;&lt;A href="https://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=n1b4cbtmb049xtn1vh9x4waiioz4.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank"&gt;Using the Hash Object&lt;/A&gt;). For example, you could load dataset Sample_Orig into a hash object (which is held in memory), then jump to certain observations (characterized by values of the "key items" of the hash object),&amp;nbsp;modify values of VAR_NAME or other variables ("data items" of the hash object) in these observations and finally write the contents of the hash object to dataset Sample_Clean.&lt;/P&gt;</description>
    <pubDate>Thu, 20 Sep 2018 08:34:44 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2018-09-20T08:34:44Z</dc:date>
    <item>
      <title>Indexing a column + lagged variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Indexing-a-column-lagged-variables/m-p/496862#M131524</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;I have two questions, which I hope can be highlighted using the image below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="smple.jpg" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/23370iDCB3C9677E3001CC/image-size/large?v=v2&amp;amp;px=999" role="button" title="smple.jpg" alt="smple.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;So&amp;nbsp;&lt;BR /&gt;1) Is there a way to index columns? For instance, if the n'th value of column Var = "Unrestricted contribution" then the [n-7]th value of "Var_Name" = "ABC", or [n+2]th value of "Var_Name" = "ABC".&lt;/P&gt;&lt;P&gt;If not, can this be done using Lagged variables?&lt;BR /&gt;&lt;BR /&gt;I tried the following:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA Sample_Clean;
	SET Sample_Orig;
	Var_Name7 = lag7(Var);
	IF Var = "unrestricted contribution" or Var = "Unrestricted Contribution" THEN Var_Name7 = lag7(VAR) ; 
	ELSE Var_Name7 = .;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But this just returns &amp;lt;.&amp;gt; for my entire column. I get that I probably need to create a loop somehow, but I am unsure!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is really appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 09:00:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Indexing-a-column-lagged-variables/m-p/496862#M131524</guid>
      <dc:creator>UniversitySas</dc:creator>
      <dc:date>2018-09-19T09:00:03Z</dc:date>
    </item>
    <item>
      <title>Re: Indexing a column + lagged variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Indexing-a-column-lagged-variables/m-p/496870#M131530</link>
      <description>&lt;P&gt;Please review the guidance on how to post a question, found under the post button.&amp;nbsp; Provide test data in the form of a datastep:&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Show what the output from this test data should be, and explain the process.&amp;nbsp; I am afraid the picture you present doesn't seem to match any of the logic, that just shows an if statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In terms of lag(), lag7() means 7 observations prior to the current observation, which for _n_ &amp;lt;= 6 is missing.&amp;nbsp; Also varname7 doesn't appeara in your screenshot, or I presume your data, therefore it will default to being created as numeric, and nothing character can be assigned, this is why you see a list of "."'s which are missing numeric.&amp;nbsp; See above, post test data and what the output should look like from that test data, you are getting yourself confused in a lot of different things here.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 09:40:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Indexing-a-column-lagged-variables/m-p/496870#M131530</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-09-19T09:40:56Z</dc:date>
    </item>
    <item>
      <title>Re: Indexing a column + lagged variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Indexing-a-column-lagged-variables/m-p/496985#M131576</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/95638"&gt;@UniversitySas&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, you can create an index for a column (variable), but in your case it seems that the "key" is just the observation number. So, you could use the POINT= option of the &lt;A href="https://documentation.sas.com/?docsetId=lestmtsref&amp;amp;docsetTarget=p00hxg3x8lwivcn1f0e9axziw57y.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank"&gt;SET statement&lt;/A&gt; for the look ahead (+7 observations) and the &lt;A href="https://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=n0l66p5oqex1f2n1quuopdvtcjqb.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank"&gt;LAG2 function&lt;/A&gt; for the look back (-2 observations).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Sample_Clean(drop=c var7);
retain c 'unrestricted contribution';
set Sample_Orig nobs=n;
p=_n_+7;
if p&amp;lt;=n then set Sample_Orig(keep=var rename=(var=var7)) point=p;
else var7=' ';
if lowcase(lag2(var))=c | lowcase(var7)=c then var_name='ABC';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Sep 2018 14:30:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Indexing-a-column-lagged-variables/m-p/496985#M131576</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-09-19T14:30:16Z</dc:date>
    </item>
    <item>
      <title>Re: Indexing a column + lagged variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Indexing-a-column-lagged-variables/m-p/497218#M131709</link>
      <description>&lt;P&gt;Thanks for your reply; this is what I was looking for.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Although, would you be able to direct me to any resources which guide through indexing a column specifically? The reason is because I plan to do this with other observations in my "Var" Column, which have a less certain criteria (e.g., contains &amp;gt;= 4 integers...)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the indexing and array examples I found seem to be to create more columns or cycle through the columns, instead of within a column.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Sep 2018 02:38:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Indexing-a-column-lagged-variables/m-p/497218#M131709</guid>
      <dc:creator>UniversitySas</dc:creator>
      <dc:date>2018-09-20T02:38:43Z</dc:date>
    </item>
    <item>
      <title>Re: Indexing a column + lagged variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Indexing-a-column-lagged-variables/m-p/497276#M131750</link>
      <description>&lt;P&gt;I think a good starting place would be &lt;A href="https://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=n06cy7dznbx6gen1q9mat8de6rdq.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank"&gt;Understanding SAS Indexes&lt;/A&gt;. With an index for a variable X (or a combination of variables) you could (among other things) use the KEY= option of the SET statement in place of the POINT= option in my example, provided that values of X characterize the target observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another powerful and flexible (though complex) tool for non-sequential retrieval and modification of data is the hash object (see&amp;nbsp;&lt;A href="https://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=n1b4cbtmb049xtn1vh9x4waiioz4.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank"&gt;Using the Hash Object&lt;/A&gt;). For example, you could load dataset Sample_Orig into a hash object (which is held in memory), then jump to certain observations (characterized by values of the "key items" of the hash object),&amp;nbsp;modify values of VAR_NAME or other variables ("data items" of the hash object) in these observations and finally write the contents of the hash object to dataset Sample_Clean.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Sep 2018 08:34:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Indexing-a-column-lagged-variables/m-p/497276#M131750</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-09-20T08:34:44Z</dc:date>
    </item>
  </channel>
</rss>

