<?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: choosing a value conditional on two different variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/choosing-a-value-conditional-on-two-different-variables/m-p/671892#M201827</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=inputdata;
by STRUCTURE_ID  YEAR_BUILT YEAR_RECONSTRUCTED Inspection_Year;

data output data;
set input data;
by STRUCTURE_ID  YEAR_BUILT YEAR_RECONSTRUCTED Inspection_Year;;
retain year_to_subtract 0;
x=lag(condition);
if first.structure_id then year_to_subtract=Year_Built;
if first.structure_id = 0 and first.year_reconstructed then do;
   if condition &amp;gt; x then year_to_subtract = Year_reconstructed;

end;
Age=inspection_year-year_to_subtract;
if ^first.YEAR_RECONSTRUCTED then Age=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 23 Jul 2020 17:18:30 GMT</pubDate>
    <dc:creator>smantha</dc:creator>
    <dc:date>2020-07-23T17:18:30Z</dc:date>
    <item>
      <title>choosing a value conditional on two different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choosing-a-value-conditional-on-two-different-variables/m-p/671621#M201700</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;Here is the scenario from the attached dataset. The variables are STRUCTURE_ID, YEAR_BUILT, YEAR_RECONSTRUCTED, Inspection_Year, and CONDITION.&amp;nbsp; Each structure is inspected as shown in the Inspection_Year column. The data are sorted based on the STRUCTURE_ID and Inspection_Year. I would like to add another column Structure_Age based on the following conditions.&lt;/P&gt;&lt;P&gt;If the structure is not reconstructed (YEAR_RECONSTRUCTED=0) or CONDITION data before reconstruction is not available then;&lt;/P&gt;&lt;P&gt;Structure_Age = Inspection_Year - YEAR_BUILT&lt;/P&gt;&lt;P&gt;If the structure is reconstructed, I want to see if the&amp;nbsp;CONDITION is improved (higher&amp;nbsp;CONDITION value than the year before&amp;nbsp; YEAR_RECONSTRUCTED in CONDITION column. then,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Structure_Age = Inspection_Year - YEAR_RECONSTRUCTED&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SAS Example.JPG" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/47552iD1935C6E1D02CAF0/image-size/large?v=v2&amp;amp;px=999" role="button" title="SAS Example.JPG" alt="SAS Example.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;If the structure is reconstructed but the CONDITION is not improved (same CONDITION value as the year before YEAR_RECONSTRUCTED in CONDITION column. then,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Structure_Age = Inspection_Year - YEAR_BUILT&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SAS Example02.JPG" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/47553i3A8381EB1687E1DD/image-size/large?v=v2&amp;amp;px=999" role="button" title="SAS Example02.JPG" alt="SAS Example02.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SAS Example03.JPG" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/47554iB6AA261D15574660/image-size/large?v=v2&amp;amp;px=999" role="button" title="SAS Example03.JPG" alt="SAS Example03.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  input STRUCTURE_ID $ YEAR_BUILT YEAR_RECONSTRUCTED INSPECTION YEAR CONDITION $ STRUCTURE_AGE;
cards;
/*STRUCTURE_AGE is 1983-1958 = 25 for CONDITION 5 below. */
1 1958 0 1983 5 25;
/*STRUCTURE_AGE is 1984-1958 = 26 for CONDITION 4 below. 
The rest of CONDITION 4 are 0, I dont need it. */
1 1958 0 1984 4 26;
1 1958 0 1985 4 0;
1 1958 0 1986 4 0;
1 1958 0 1987 4 0;
1 1958 0 1988 4 0;
/* The structure is reconstructed in 1992 and CONDITION is improved in 1993 inspection
and it is different than 1988 CONDITION 4, therefore STRUCTIRE_AGE is 1993-1992 =1 as below.
For the rest of CONDITION 8, STRUCTURE_AGE = 0, I dont need it. */
1 1958 1992 1993 8 1;
1 1958 1992 1994 8 0;
1 1958 1992 1995 8 0;
/* The CONDITION drops to 7 in 1996 and STRCUTURE_AGE= 1196-1992 = 4 as below.
STRUCTURE_AGE for the rest of CONDITION 7 is 0, I dont need it. if the CONDIION changes
to 6, I need STRUCTURE_AGE to be computed, if not to be 0. */
1 1958 1992 1996 7 4;
1 1958 1992 1997 7 0;
1 1958 1992 1998 7 0;
1 1958 1992 1999 7 0;
1 1958 1992 2000 7 0;
1 1958 1992 2001 7 0;
/* The CONDITION in 1983 is 6 and STRUTURE_AGE = 1983 - 1937 = 46 and for the rest
of CODNTION 6 it is 0 as I don't need it. in 1990 the CONDIION is 5 and STRUCTURE_AGE
1990 - 1937 = 53 and for the rest of CONDITION 5 it is 0 as below. */
10027 1937 0 1983 6 46;
10027 1937 0 1984 6 0;
10027 1937 0 1985 6 0;
10027 1937 0 1986 6 0;
10027 1937 0 1987 6 0;
10027 1937 0 1988 6 0;
10027 1937 0 1989 6 0;
10027 1937 0 1990 5 53;
10027 1937 0 1991 5 0;
10027 1937 0 1992 5 0;
10027 1937 0 1993 5 0;
10027 1937 0 1994 5 0;
/* The bridge is reconstructed in 1994, but CONDITION is not improved in 1995, as it is
the same CONDITION as 1994 inspection,therefore STRCUTURE_AGE is 0 as below.*/
10027 1937 1994 1995 5 0;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I appreciate your time and help!&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jul 2020 16:44:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choosing-a-value-conditional-on-two-different-variables/m-p/671621#M201700</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2020-07-23T16:44:31Z</dc:date>
    </item>
    <item>
      <title>Re: choosing a value conditional on two different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choosing-a-value-conditional-on-two-different-variables/m-p/671630#M201703</link>
      <description>&lt;P&gt;Please provide, in a SAS data step form, how you want the output data to look.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/253384"&gt;@mmhxc5&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;If the structure is not reconstructed (YEAR_RECONSTRUCTED=0) then,&amp;nbsp; Structure_Age = Inspection_Year - YEAR_BUILT &lt;EM&gt;&lt;STRONG&gt;for each first distinct CONDITION&lt;/STRONG&gt;&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the structure is reconstructed, I wan to see if the&amp;nbsp;CONDITION is improved (higher&amp;nbsp;CONDITION value than the year before&amp;nbsp; YEAR_RECONSTRUCTED. then,&amp;nbsp; Structure_Age = Inspection_Year - YEAR_RECONSTRUCTED, &lt;EM&gt;&lt;STRONG&gt;for each first distinct CONDITION.&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the structure is reconstructed but the CONDITION is not improved (same CONDITION value as the year before YEAR_RECONSTRUCTED. then,&amp;nbsp; Structure_Age = Inspection_Year - YEAR_BUILT, &lt;EM&gt;&lt;STRONG&gt;for each first distinct CONDITION.&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If a bridge is reconstructed but CONDITION data before reconstruction is not available then, Structure_Age = Inspection_Year - YEAR_BUILT, because I don't know if the reconstruction was effective.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You almost alway want the structure_age "&lt;EM&gt;&lt;STRONG&gt;for each first distinct condition&lt;/STRONG&gt;&lt;/EM&gt;".&amp;nbsp; What do you want for all the other records?&amp;nbsp;&amp;nbsp; This question could be answered with an output data set.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 21:55:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choosing-a-value-conditional-on-two-different-variables/m-p/671630#M201703</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-07-22T21:55:35Z</dc:date>
    </item>
    <item>
      <title>Re: choosing a value conditional on two different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choosing-a-value-conditional-on-two-different-variables/m-p/671664#M201725</link>
      <description>&lt;P&gt;Some parts of your explanation are very fuzzy to me but will try to do the problem anyway&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=inputdata;
by STRUCTURE_ID  YEAR_BUILT YEAR_RECONSTRUCTED Inspection_Year;&lt;BR /&gt;data output data;&lt;BR /&gt;set input data;&lt;BR /&gt;by STRUCTURE_ID  YEAR_BUILT YEAR_RECONSTRUCTED Inspection_Year;;
retain year_to_subtract 0;
x=lag(condition);
if first.structure_id then year_to_subtract=Year_Built;
if first.structure_id = 0 and first.year_reconstructed then do;
   if condition &amp;gt; x then year_to_subtract = Year_reconstructed;

end;
Age=inspection_year-year_to_subtract;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jul 2020 12:19:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choosing-a-value-conditional-on-two-different-variables/m-p/671664#M201725</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-07-23T12:19:19Z</dc:date>
    </item>
    <item>
      <title>Re: choosing a value conditional on two different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choosing-a-value-conditional-on-two-different-variables/m-p/671667#M201728</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214340"&gt;@smantha&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Some parts of your explanation are very fuzzy to me but will try to do the problem anyway&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=inputdata;
by STRUCTURE_ID  YEAR_BUILT YEAR_RECONSTRUCTED Inspection_Year;
retain year_to_subtract 0;
x=lag(condition);
if first.structure_id then year_to_subtract=Year_Built;
if first.structure_id = 0 and first.year_reconstructed then do;
   if condition &amp;gt; x then year_to_subtract = Year_reconstructed;

end;
Age=inspection_year-year_to_subtract;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It is - at least for my internal clock - very, very early in the morning, so maybe i miss something, but afaik proc sort does not support if-statement and everything else after BY-statement.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jul 2020 05:07:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choosing-a-value-conditional-on-two-different-variables/m-p/671667#M201728</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-07-23T05:07:15Z</dc:date>
    </item>
    <item>
      <title>Re: choosing a value conditional on two different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choosing-a-value-conditional-on-two-different-variables/m-p/671788#M201783</link>
      <description>&lt;P&gt;thank you for pointing that out. Corrected my code to what is required.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jul 2020 12:20:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choosing-a-value-conditional-on-two-different-variables/m-p/671788#M201783</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-07-23T12:20:30Z</dc:date>
    </item>
    <item>
      <title>Re: choosing a value conditional on two different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choosing-a-value-conditional-on-two-different-variables/m-p/671884#M201822</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;, thank you for your help. Please see the output I want in my original post.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jul 2020 16:47:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choosing-a-value-conditional-on-two-different-variables/m-p/671884#M201822</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2020-07-23T16:47:16Z</dc:date>
    </item>
    <item>
      <title>Re: choosing a value conditional on two different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choosing-a-value-conditional-on-two-different-variables/m-p/671892#M201827</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=inputdata;
by STRUCTURE_ID  YEAR_BUILT YEAR_RECONSTRUCTED Inspection_Year;

data output data;
set input data;
by STRUCTURE_ID  YEAR_BUILT YEAR_RECONSTRUCTED Inspection_Year;;
retain year_to_subtract 0;
x=lag(condition);
if first.structure_id then year_to_subtract=Year_Built;
if first.structure_id = 0 and first.year_reconstructed then do;
   if condition &amp;gt; x then year_to_subtract = Year_reconstructed;

end;
Age=inspection_year-year_to_subtract;
if ^first.YEAR_RECONSTRUCTED then Age=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Jul 2020 17:18:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choosing-a-value-conditional-on-two-different-variables/m-p/671892#M201827</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-07-23T17:18:30Z</dc:date>
    </item>
    <item>
      <title>Re: choosing a value conditional on two different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choosing-a-value-conditional-on-two-different-variables/m-p/671962#M201855</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214340"&gt;@smantha&lt;/a&gt; , I revised a little bit of your code as below and it worked.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.structure_id = 0 and first.year_reconstructed then do;
   if condition &amp;gt; x and year_reconstructed ne 0 then year_to_subtract = Year_reconstructed;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks for your help!&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jul 2020 21:29:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choosing-a-value-conditional-on-two-different-variables/m-p/671962#M201855</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2020-07-23T21:29:30Z</dc:date>
    </item>
  </channel>
</rss>

