<?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: Retaining first variable in column for future use in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retaining-first-variable-in-column-for-future-use/m-p/911482#M359418</link>
    <description>&lt;P&gt;You seem to be using VARIABLE/ROW/COLUMN in a confusing way.&amp;nbsp; A SAS dataset consists of a set of OBSERVATIONS that each have&amp;nbsp; VALUES for all of the VARIABLES in the dataset.&amp;nbsp; A ROW could be considered the same as an observation.&amp;nbsp; A COLUMN could be considered the same as a VARIABLE.&amp;nbsp; But you cannot have a first variable in a variable (column).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sounds like you want to retain the value from the first OBSERVATION onto all of the following observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The simplest way is to read in the dataset twice once with just the first observation and only that one variable and the second time without that variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if _n_=1 then set have(keep=X);
  set have(drop=X);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is no utility in trying to retain a variable that is coming in from a dataset.&amp;nbsp; First of all, such variable as already retained.&amp;nbsp; But more importantly the retained value is replaced when the next observation is read.&amp;nbsp; So you will need to make a NEW variable so you can RETAIN the first value.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't need the original variable (the one with the zeros) then DROP it.&amp;nbsp; If you would like the new variable to use the same name as the old then add a RENAME statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  if _n_=1 then newvar=x;
  retain newvar;
  drop x;
  rename newvar=x;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will have to use the second option if you have some other grouping variable so that you want to carry forward the first value for each group rather than just the value from the first observation in the dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by group;
  if first.group then newvar=x;
  retain newvar;
  drop x;
  rename newvar=x;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 13 Jan 2024 21:13:00 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-01-13T21:13:00Z</dc:date>
    <item>
      <title>Retaining first variable in column for future use</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-first-variable-in-column-for-future-use/m-p/911481#M359417</link>
      <description>&lt;P&gt;Hi, I have a column that is partially populated in which the first row contains a non-zero value and the remaining rows contain zeros.&amp;nbsp; I would like to update the rows containing&amp;nbsp; zeros with the &lt;EM&gt;first value&lt;/EM&gt; in the row.&amp;nbsp; &amp;nbsp;I have tried using retain and FIRST.value functions to no avail.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the example below first row contains 3.&amp;nbsp; &amp;nbsp;I need SAS to read this first value, retain it and update the&amp;nbsp;&lt;/P&gt;&lt;P&gt;remaining rows with 3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3&lt;/P&gt;&lt;P&gt;0&lt;/P&gt;&lt;P&gt;0&lt;/P&gt;&lt;P&gt;0&lt;/P&gt;&lt;P&gt;0&lt;/P&gt;&lt;P&gt;0&lt;/P&gt;</description>
      <pubDate>Sat, 13 Jan 2024 18:40:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-first-variable-in-column-for-future-use/m-p/911481#M359417</guid>
      <dc:creator>cyatkp</dc:creator>
      <dc:date>2024-01-13T18:40:27Z</dc:date>
    </item>
    <item>
      <title>Re: Retaining first variable in column for future use</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-first-variable-in-column-for-future-use/m-p/911482#M359418</link>
      <description>&lt;P&gt;You seem to be using VARIABLE/ROW/COLUMN in a confusing way.&amp;nbsp; A SAS dataset consists of a set of OBSERVATIONS that each have&amp;nbsp; VALUES for all of the VARIABLES in the dataset.&amp;nbsp; A ROW could be considered the same as an observation.&amp;nbsp; A COLUMN could be considered the same as a VARIABLE.&amp;nbsp; But you cannot have a first variable in a variable (column).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sounds like you want to retain the value from the first OBSERVATION onto all of the following observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The simplest way is to read in the dataset twice once with just the first observation and only that one variable and the second time without that variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if _n_=1 then set have(keep=X);
  set have(drop=X);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is no utility in trying to retain a variable that is coming in from a dataset.&amp;nbsp; First of all, such variable as already retained.&amp;nbsp; But more importantly the retained value is replaced when the next observation is read.&amp;nbsp; So you will need to make a NEW variable so you can RETAIN the first value.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't need the original variable (the one with the zeros) then DROP it.&amp;nbsp; If you would like the new variable to use the same name as the old then add a RENAME statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  if _n_=1 then newvar=x;
  retain newvar;
  drop x;
  rename newvar=x;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will have to use the second option if you have some other grouping variable so that you want to carry forward the first value for each group rather than just the value from the first observation in the dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by group;
  if first.group then newvar=x;
  retain newvar;
  drop x;
  rename newvar=x;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 13 Jan 2024 21:13:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-first-variable-in-column-for-future-use/m-p/911482#M359418</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-01-13T21:13:00Z</dc:date>
    </item>
    <item>
      <title>Re: Retaining first variable in column for future use</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-first-variable-in-column-for-future-use/m-p/911483#M359419</link>
      <description>&lt;P&gt;So you want to carry forward the&amp;nbsp;&lt;EM&gt;value&lt;/EM&gt; of a&amp;nbsp;&lt;EM&gt;variable&lt;/EM&gt; from the first&amp;nbsp;&lt;EM&gt;observation&lt;/EM&gt; to following&amp;nbsp;&lt;EM&gt;observations&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;(this is how it's written with SAS; in a database context, you would use column and row instead of variable and observation).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is it really only the &lt;EM&gt;first observation of the dataset&lt;/EM&gt; you want to carry forward, or should it be &lt;EM&gt;the first observation of a group&lt;/EM&gt; carried forward &lt;EM&gt;within that group&lt;/EM&gt;? If yes, show us a better example of your dataset so we can see how groups are identified.&lt;/P&gt;
&lt;P&gt;You also say you want to update the zero values; should non-zero values remain as they are?&lt;/P&gt;</description>
      <pubDate>Sat, 13 Jan 2024 20:08:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-first-variable-in-column-for-future-use/m-p/911483#M359419</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-01-13T20:08:17Z</dc:date>
    </item>
    <item>
      <title>Re: Retaining first variable in column for future use</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-first-variable-in-column-for-future-use/m-p/911497#M359423</link>
      <description>&lt;P&gt;To just retain any value greater zero and repeat until the next value greater zero below code should do.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input var;
  datalines;
3
0
0
0
4
0
;

data want(drop=_:);
  set have;
  retain _tmp_var;
  if var&amp;gt;0 then _tmp_var=var;
  var=_tmp_var;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1705197985474.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92524iA51816D04BCA7FEB/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1705197985474.png" alt="Patrick_0-1705197985474.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;If you need something like above by group then just have your source data sorted by group and use by group processing with if first.&amp;lt;group_var&amp;gt; then ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jan 2024 02:07:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-first-variable-in-column-for-future-use/m-p/911497#M359423</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-01-14T02:07:10Z</dc:date>
    </item>
    <item>
      <title>Re: Retaining first variable in column for future use</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retaining-first-variable-in-column-for-future-use/m-p/911512#M359427</link>
      <description>&lt;P&gt;Or this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
_n_=var;
set have;
if var=0 then var=_n_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 14 Jan 2024 10:00:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retaining-first-variable-in-column-for-future-use/m-p/911512#M359427</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-01-14T10:00:23Z</dc:date>
    </item>
  </channel>
</rss>

