<?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: Identify record with negative value in a specific variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Identify-record-with-negative-value-in-a-specific-variable/m-p/567472#M159577</link>
    <description>&lt;P&gt;I like the terse 2-pass solution by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;. However, it can also be done in a single pass, while avoiding unnecessary computations:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                       
  input var1: $1. var2 var3 ;                                     
  cards ;                                                         
A 1  157                                                          
A 2  130                                                          
A 3   67                                                          
A 4  -54                                                          
A 5  -89                                                          
A 6 -101                                                          
B 1  500                                                          
B 2  456                                                          
B 3  234                                                          
B 4  -10                                                          
B 5 -303                                                          
B 6 -409                                                          
C 1  239                                                          
C 2  210                                                          
C 3  150                                                          
C 4   -2                                                          
C 5  -30                                                          
C 6  -44                                                          
run ;                                                             
                                                                  
data need ;                                                       
  do until (last.var1) ;                                          
    set have ;                                                    
    by var1 ;                                                     
    if N (var) then continue ;                                    
    if var3 &amp;lt; 0 then do ;                                         
      var = var2 + (var2 - _var2) * divide (_var3, _var3 - var3) ;
      output ;                                                    
    end ;                                                         
    _var2 = var2 ;                                                
    _var3 = var3 ;                                                
  end ;                                                           
run ;                                                             
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
    <pubDate>Wed, 19 Jun 2019 22:18:58 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2019-06-19T22:18:58Z</dc:date>
    <item>
      <title>Identify record with negative value in a specific variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-record-with-negative-value-in-a-specific-variable/m-p/567361#M159538</link>
      <description>&lt;P&gt;Hello everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is an example for which I have some difficulties:&lt;/P&gt;&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;length VAR1 $ 1; input VAR1 $ VAR2 VAR3;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;A 1 157&lt;/P&gt;&lt;P&gt;A 2 130&lt;/P&gt;&lt;P&gt;A 3 67&lt;/P&gt;&lt;P&gt;A 4 -54&lt;/P&gt;&lt;P&gt;A 5 -89&lt;/P&gt;&lt;P&gt;A 6 -101&lt;/P&gt;&lt;P&gt;B 1 500&lt;/P&gt;&lt;P&gt;B 2 456&lt;/P&gt;&lt;P&gt;B 3 234&lt;/P&gt;&lt;P&gt;B 4 -10&lt;/P&gt;&lt;P&gt;B 5 -303&lt;/P&gt;&lt;P&gt;B 6 -409&lt;/P&gt;&lt;P&gt;C 1 239&lt;/P&gt;&lt;P&gt;C 2 210&lt;/P&gt;&lt;P&gt;C 3 150&lt;/P&gt;&lt;P&gt;C 4 -2&lt;/P&gt;&lt;P&gt;C 5 -30&lt;/P&gt;&lt;P&gt;C 6 -44&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The number of records for A, B, C etc is fixed and equal to 6.&lt;/P&gt;&lt;P&gt;VAR3 is sorted from high to low and contains negative values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The steps to perform are the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) For each VAR1 (A, B, C etc) identify VAR2 and VAR3 for which VAR3 is negative for the first time&lt;/P&gt;&lt;P&gt;2) Identify VAR2 and VAR3 of the record just above&lt;/P&gt;&lt;P&gt;3) Make an operation with values obtained from 1) and 2). See example below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example: for VAR1 = A&lt;/P&gt;&lt;P&gt;1) VAR2 = 4 VAR3=-54&lt;/P&gt;&lt;P&gt;2) VAR2 = 3 VAR3=67&lt;/P&gt;&lt;P&gt;3) Make an operation with 1) and 2):&amp;nbsp; &amp;nbsp; &amp;nbsp; 4+ (4-3)*[67/(67-(-54))]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2019 17:05:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-record-with-negative-value-in-a-specific-variable/m-p/567361#M159538</guid>
      <dc:creator>Spawn77</dc:creator>
      <dc:date>2019-06-19T17:05:05Z</dc:date>
    </item>
    <item>
      <title>Re: Identify record with negative value in a specific variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-record-with-negative-value-in-a-specific-variable/m-p/567368#M159541</link>
      <description>&lt;P&gt;Welcome to the SAS Communities &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; If your data represents your actual problem, then do something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
length VAR1 $ 1; input VAR1 $ VAR2 VAR3;
datalines;
A 1 157
A 2 130
A 3 67
A 4 -54
A 5 -89
A 6 -101
B 1 500
B 2 456
B 3 234
B 4 -10
B 5 -303
B 6 -409
C 1 239
C 2 210
C 3 150
C 4 -2
C 5 -30
C 6 -44
;

data temp;
   merge test test(firstobs=2 rename=(VAR2=_VAR2 VAR3=_VAR3));
   newvar=_VAR2+(_VAR2-VAR2)*(VAR3 / (VAR3-_VAR3));
   if VAR3 gt 0 &amp;amp; _VAR3 lt 0;
   format newvar 8.2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Jun 2019 17:24:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-record-with-negative-value-in-a-specific-variable/m-p/567368#M159541</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-06-19T17:24:39Z</dc:date>
    </item>
    <item>
      <title>Re: Identify record with negative value in a specific variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-record-with-negative-value-in-a-specific-variable/m-p/567472#M159577</link>
      <description>&lt;P&gt;I like the terse 2-pass solution by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;. However, it can also be done in a single pass, while avoiding unnecessary computations:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                       
  input var1: $1. var2 var3 ;                                     
  cards ;                                                         
A 1  157                                                          
A 2  130                                                          
A 3   67                                                          
A 4  -54                                                          
A 5  -89                                                          
A 6 -101                                                          
B 1  500                                                          
B 2  456                                                          
B 3  234                                                          
B 4  -10                                                          
B 5 -303                                                          
B 6 -409                                                          
C 1  239                                                          
C 2  210                                                          
C 3  150                                                          
C 4   -2                                                          
C 5  -30                                                          
C 6  -44                                                          
run ;                                                             
                                                                  
data need ;                                                       
  do until (last.var1) ;                                          
    set have ;                                                    
    by var1 ;                                                     
    if N (var) then continue ;                                    
    if var3 &amp;lt; 0 then do ;                                         
      var = var2 + (var2 - _var2) * divide (_var3, _var3 - var3) ;
      output ;                                                    
    end ;                                                         
    _var2 = var2 ;                                                
    _var3 = var3 ;                                                
  end ;                                                           
run ;                                                             
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2019 22:18:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-record-with-negative-value-in-a-specific-variable/m-p/567472#M159577</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-06-19T22:18:58Z</dc:date>
    </item>
  </channel>
</rss>

