<?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 new column based on another column's value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-column-based-on-another-column-s-value/m-p/599350#M173002</link>
    <description>&lt;P&gt;Hello mkeintz,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks very much, your solution and idea is very valuable.&lt;/P&gt;&lt;P&gt;If we define status_1 and status as character value first, then it solved my question! otherwise it will cause error such as "invalid numeric data, "Unknown" ".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 25 Oct 2019 14:51:01 GMT</pubDate>
    <dc:creator>glz</dc:creator>
    <dc:date>2019-10-25T14:51:01Z</dc:date>
    <item>
      <title>create a new column based on another column's value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-column-based-on-another-column-s-value/m-p/599120#M172881</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have an example data in below, the column status (highlight with yellow) is what I want.&lt;/P&gt;&lt;P&gt;For each subject, once "Positive" occur in Test, then the status for this subject is "Positive";&lt;/P&gt;&lt;P&gt;If no Positive, there is "Negative" then the status for this subject is "Negative";&lt;/P&gt;&lt;P&gt;If no positive, there are "Unknown" and one other character string such as "&amp;lt;5" then the status is "&amp;lt;5";&lt;/P&gt;&lt;P&gt;if no positive, there are&amp;nbsp;"Unknown" and many other character string, such as "&amp;lt;5" and "&amp;lt;10", then the status is "NA";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help me with this problem?&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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 233px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/33385iE498FB5E7EF349AA/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Oct 2019 18:10:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-new-column-based-on-another-column-s-value/m-p/599120#M172881</guid>
      <dc:creator>glz</dc:creator>
      <dc:date>2019-10-24T18:10:10Z</dc:date>
    </item>
    <item>
      <title>Re: create a new column based on another column's value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-column-based-on-another-column-s-value/m-p/599137#M172891</link>
      <description>&lt;P&gt;Here are your rules as I understand them;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For each ID&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;If there is any test='Positive' then status='Positive'.&lt;/LI&gt;
&lt;LI&gt;Otherwise if there is any test='Negative' then status='Negative'&lt;/LI&gt;
&lt;LI&gt;Otherwise if all of the tests='Unknown' then status='Unknown'&lt;/LI&gt;
&lt;LI&gt;Otherwise if there is a mix of 'Unknown' and a single test value, then status=that test value&lt;/LI&gt;
&lt;LI&gt;Otherwise if there is more than one test value, the status='NA'&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;The program below has not been tested, because your picture of the data is an image, not text.&amp;nbsp; I'm not going to manually type in your data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=any_:  n_unknown n_tests status_1);
  set have (in=first_pass)
      have (in=second_pass);
  by id;
  retain status;&lt;BR /&gt;  if 0 then status_1=status;  /* Edited addition */
  retain any_pos any_neg n_unknown n_tests status_1;
  if first.id then call missing(status,any_pos,any_neg,n_unknown,n_tests,status_1);
  if first_pass=1 then do;
    n_tests+1;
    if test='Positive' then any_pos=1; else
    if test='Negative' then any_net=1; else
    if test='Unknown'  then n_unknown+1; else
    do;
      if status_1=' ' then status_1=test;
      else if status_1^=test then status_1='NA';
    end;
    if any_pos=1         then status='Positive'; else
    if any_neg=1         then status='Negative'; else
    if n_unknown=n_tests then status='Unknown';  else
    status=status_1;
  end;
  if second_pass=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This works by passing through each ID twice&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;The first pass&lt;/STRONG&gt; reads each record once.&amp;nbsp; It determines the presence of any Positive, or Negative tests, and it counts the number of 'Unknown' results, and maintains a STATUS_1 variable.&amp;nbsp; STATUS_1 will get the first measured result (i.e. not Negative, Positive, or Unknown), and it will be compared to all subsequent measured results.&amp;nbsp; If any of then are different then STATUS_1 will be set to 'NA'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The desired STATUS variable is updated with each first pass record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;The second pass&lt;/STRONG&gt; does nothing but inherit the value of STATUS determined in the first pass.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited change: I added the statement&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if 0 then status_1=status;&lt;/P&gt;
&lt;P&gt;so that STATUS_1 would be a character variable matching the length of STATUS.&lt;/P&gt;
&lt;P&gt;The condition "if 0" is never true, so the actual value of STATUS_1 is not changed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 17:46:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-new-column-based-on-another-column-s-value/m-p/599137#M172891</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-10-25T17:46:19Z</dc:date>
    </item>
    <item>
      <title>Re: create a new column based on another column's value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-column-based-on-another-column-s-value/m-p/599228#M172926</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/284702"&gt;@glz&lt;/a&gt;: As &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;has mentioned, it would be nice of you if you provided a step for creating you sample input data, thus ridding those kind enough to help you from the need to reconcoct it themselves (this what the first step below does). Having said that, try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have (keep = id test) ;                                                                                                            
  array v [5  ] $8 _temporary_ ("Positive","Negative","Unknown","&amp;lt;5","&amp;lt;10") ;                                                           
  array t [8,4]  8 _temporary_                                                                                                          
  (3 3 1 1  3 3 2 2  1 1 2 2  1 1 4 4  3 3 4 4  3 3 4 5  3 3 3 3  1 2 3 1) ;                                                            
  do i = 1 to 8 ;                                                                                                                       
    ID = "A" || put (i,z3.) ;                                                                                                           
    do j = 1 to 4 ;                                                                                                                     
      Test = v [ t[i,j]] ;                                                                                                              
      output ;                                                                                                                          
    end ;                                                                                                                               
  end ;                                                                                                                                 
run ;                                                                                                                                   
                                                                                                                                        
data want (drop = _:) ;                                                                                                                 
  do _n_ = 1 by 1 until (last.id) ;                                                                                                     
    set have ;                                                                                                                          
    by id ;                                                                                                                             
    array f[0:4] _f0-_f4 ;                                                                                                              
    f [find ("PNU5", char (compress (test, "&amp;lt;"), 1))] = 1 ;                                                                             
  end ;                                                                                                                                 
  do _n_ = 1 to _n_ ;                                                                                                                   
    set have ;                                                                                                                          
    if      _f1 then status = "Positive" ;                                                                                              
    else if _f2 then status = "Negative" ;                                                                                              
    else if _f3 then do ;                                                                                                               
      if      _f0 then status = "N/A" ;                                                                                                 
      else if _f4 then status = "&amp;lt;5" ;                                                                                                  
      else status = "Unknown" ;                                                                                                         
    end ;                                                                                                                               
    output ;                                                                                                                            
  end ;                                                                                                                                 
run ;                      
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 03:27:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-new-column-based-on-another-column-s-value/m-p/599228#M172926</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-10-25T03:27:41Z</dc:date>
    </item>
    <item>
      <title>Re: create a new column based on another column's value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-column-based-on-another-column-s-value/m-p/599350#M173002</link>
      <description>&lt;P&gt;Hello mkeintz,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks very much, your solution and idea is very valuable.&lt;/P&gt;&lt;P&gt;If we define status_1 and status as character value first, then it solved my question! otherwise it will cause error such as "invalid numeric data, "Unknown" ".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 14:51:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-new-column-based-on-another-column-s-value/m-p/599350#M173002</guid>
      <dc:creator>glz</dc:creator>
      <dc:date>2019-10-25T14:51:01Z</dc:date>
    </item>
  </channel>
</rss>

