<?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: Create a variable dependent on value from another row in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-variable-dependent-on-value-from-another-row/m-p/861611#M82540</link>
    <description>&lt;P&gt;It is never going to be missing with that data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm" target="_self"&gt;SUM STATEMENT&lt;/A&gt;&amp;nbsp;means that the variable will be initialized to zero.&amp;nbsp; Plus for the first observation of each BY group it is set to zero.&lt;/P&gt;</description>
    <pubDate>Tue, 28 Feb 2023 22:14:46 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-02-28T22:14:46Z</dc:date>
    <item>
      <title>Create a variable dependent on value from another row</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-variable-dependent-on-value-from-another-row/m-p/861246#M82534</link>
      <description>&lt;P&gt;We have this dataset. Each ID is an item sold at a store. OLD_NEW is whether the version of the item is old or new. Months is time it took for the item to sell.&amp;nbsp; Type1 is equal to A if it got sold before 12 months.&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;STORE&lt;/TD&gt;&lt;TD&gt;OLD_NEW&lt;/TD&gt;&lt;TD&gt;MONTHS&lt;/TD&gt;&lt;TD&gt;type1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;OLD&lt;/TD&gt;&lt;TD&gt;34&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;NEW&lt;/TD&gt;&lt;TD&gt;11&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;102&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;OLD&lt;/TD&gt;&lt;TD&gt;45&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;103&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;NEW&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;104&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;NEW&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;105&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;OLD&lt;/TD&gt;&lt;TD&gt;34&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;We would like to create a variable type2 that will only be filled out if the other item in the same store had type1=A. Like the following:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;STORE&lt;/TD&gt;&lt;TD&gt;OLD_NEW&lt;/TD&gt;&lt;TD&gt;MONTHS&lt;/TD&gt;&lt;TD&gt;type1&lt;/TD&gt;&lt;TD&gt;type2&lt;/TD&gt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t5/SAS-Procedures/Create-a-variable-dependent-on-value-from-another-row/m-p/861246#M82534</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-02-27T23:04:06Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable dependent on value from another row</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-variable-dependent-on-value-from-another-row/m-p/861255#M82535</link>
      <description>&lt;P&gt;Why did you show the observations in random order?&amp;nbsp; It would be much easier if they were in order, then you can just remember if there have been any with months &amp;lt; 12.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I find binary flags much easier to deal with.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID STORE OLD_NEW $ MONTHS type1 $ type2 $;
cards;
100 1 OLD 34 . B
101 1 NEW 11 A .
102 2 OLD 45 . .
103 2 NEW 15 . .
104 3 NEW 6 A .
105 3 OLD 34 . B
;

proc sort data=have ;
  by store months ;
run;

data want;
  set have;
  by store ;
  if first.store then n_type1=0;
  new_type1=(months &amp;lt; 12);
  new_type2=(n_type1&amp;gt;0);
  if new_type1 then n_type1+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                                                                          new_     new_
OBS     ID    STORE    OLD_NEW    MONTHS    type1    type2    n_type1    type1    type2

 1     101      1        NEW        11        A                  1         1        0
 2     100      1        OLD        34                 B         1         0        1
 3     103      2        NEW        15                           0         0        0
 4     102      2        OLD        45                           0         0        0
 5     104      3        NEW         6        A                  1         1        0
 6     105      3        OLD        34                 B         1         0        1
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 01:18:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-variable-dependent-on-value-from-another-row/m-p/861255#M82535</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-02-28T01:18:22Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable dependent on value from another row</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-variable-dependent-on-value-from-another-row/m-p/861522#M82536</link>
      <description>&lt;P&gt;Thank you! It works perfectly.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 18:01:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-variable-dependent-on-value-from-another-row/m-p/861522#M82536</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-02-28T18:01:17Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable dependent on value from another row</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-variable-dependent-on-value-from-another-row/m-p/861594#M82538</link>
      <description>Could you please explain why n_type1 turns into 1 for the second observation by adding the last line of code if it is missing up until that line?</description>
      <pubDate>Tue, 28 Feb 2023 21:13:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-variable-dependent-on-value-from-another-row/m-p/861594#M82538</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-02-28T21:13:39Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable dependent on value from another row</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-variable-dependent-on-value-from-another-row/m-p/861611#M82540</link>
      <description>&lt;P&gt;It is never going to be missing with that data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm" target="_self"&gt;SUM STATEMENT&lt;/A&gt;&amp;nbsp;means that the variable will be initialized to zero.&amp;nbsp; Plus for the first observation of each BY group it is set to zero.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 22:14:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-variable-dependent-on-value-from-another-row/m-p/861611#M82540</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-02-28T22:14:46Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable dependent on value from another row</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-variable-dependent-on-value-from-another-row/m-p/861620#M82541</link>
      <description>Thank you! Sorry, I am new with SAS and I want to understand the code instead of only using. What is the function of the n_type1? And why for the observation 2 is 1 if you have this line " if new_type1 then n_type1+1;" Doesn't it mean that it is 1 if new_type1 is real? Why line is 0 for n_type1?</description>
      <pubDate>Tue, 28 Feb 2023 22:37:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-variable-dependent-on-value-from-another-row/m-p/861620#M82541</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-02-28T22:37:48Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable dependent on value from another row</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-variable-dependent-on-value-from-another-row/m-p/861626#M82542</link>
      <description>&lt;P&gt;Read the documentation on the SUM STATEMENT that I linked before. Also read about the RETAIN statement, using a SUM statement implies that the variable that is being summed into is retained.&amp;nbsp; Also read about difference in how missing values are handled by normal addition operator (A+B) and the SUM() function, sum(a,b).&amp;nbsp; The SUM STATEMENT handles missing values of its two arguments in the same way as the SUM() function does.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also understand about how Boolean expressions work in SAS. When you use a number, like NEW_TYPE1, in a boolean expression then SAS will treat a value that is 0 or missing as FALSE and any other value as TRUE.&amp;nbsp; When you use the result of a Boolean expression, such as (months&amp;lt;12), as a number then TRUE expression result in a 1 and FALSE expressions result in a 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if new_type1 then n_type1+1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;means.&lt;/P&gt;
&lt;P&gt;When NEW_TYPE1 has a value that is neither zero or missing then add 1 to the current value on N_TYPE1 (and if N_TYPE1 was somehow set to missing the result is 1 instead of missing).&amp;nbsp; Or more clearly:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Increment n_type1 by 1 when new_type1 is true.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Tue, 28 Feb 2023 23:46:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-variable-dependent-on-value-from-another-row/m-p/861626#M82542</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-02-28T23:46:30Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable dependent on value from another row</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-variable-dependent-on-value-from-another-row/m-p/861639#M82545</link>
      <description>Thank you, this is very helpful.</description>
      <pubDate>Wed, 01 Mar 2023 02:24:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-variable-dependent-on-value-from-another-row/m-p/861639#M82545</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-03-01T02:24:25Z</dc:date>
    </item>
  </channel>
</rss>

