<?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 if values stored in different variables are not equal then output in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/if-values-stored-in-different-variables-are-not-equal-then/m-p/884444#M349413</link>
    <description>&lt;P&gt;Dear all,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if I have a data that looks like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines;
	input Treatment $10. Visit1 $10. Visit2 $14. Visit3 $15.;
	datalines;
chemo	  completed	completed     completed
Immune	  ongoing	ongoing	      ongoing
Stem cell cancelled	cancelled     cancelled
chemo	  ongoing	completed     cancelled
Targeted  ongoing	starting soon ongoing
Brachy	  cancelled	cancelled	  cancelled
surgery	  cancelled	ongoing	      starting soon
chemo	  completed	starting soon completed
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to look through the variable treatment and any time I find chemo I will like to output all&amp;nbsp; values of subsequent variables. If they are equal then this should be outputted once but if not equal the I will like to output all values. Here is my start code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
if find(treatment, "chemo")&amp;gt;0 then do;
if visit1=visit2 and visit2=visit3 then status=visit1;
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;for example in the first observation I find chemo, so I will check if the values of visit1-visit3 are equal. In this case yes, so the value completed is outputted once.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the fourth observation I find chemo but the values of visit1-visit3 are not equal so I will like to output status=&lt;CODE class=" language-sas"&gt;ongoing,completed and cancelled&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;Any&amp;nbsp;help?&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 12 Jul 2023 07:35:51 GMT</pubDate>
    <dc:creator>Anita_n</dc:creator>
    <dc:date>2023-07-12T07:35:51Z</dc:date>
    <item>
      <title>if values stored in different variables are not equal then output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-values-stored-in-different-variables-are-not-equal-then/m-p/884444#M349413</link>
      <description>&lt;P&gt;Dear all,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if I have a data that looks like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines;
	input Treatment $10. Visit1 $10. Visit2 $14. Visit3 $15.;
	datalines;
chemo	  completed	completed     completed
Immune	  ongoing	ongoing	      ongoing
Stem cell cancelled	cancelled     cancelled
chemo	  ongoing	completed     cancelled
Targeted  ongoing	starting soon ongoing
Brachy	  cancelled	cancelled	  cancelled
surgery	  cancelled	ongoing	      starting soon
chemo	  completed	starting soon completed
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to look through the variable treatment and any time I find chemo I will like to output all&amp;nbsp; values of subsequent variables. If they are equal then this should be outputted once but if not equal the I will like to output all values. Here is my start code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
if find(treatment, "chemo")&amp;gt;0 then do;
if visit1=visit2 and visit2=visit3 then status=visit1;
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;for example in the first observation I find chemo, so I will check if the values of visit1-visit3 are equal. In this case yes, so the value completed is outputted once.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the fourth observation I find chemo but the values of visit1-visit3 are not equal so I will like to output status=&lt;CODE class=" language-sas"&gt;ongoing,completed and cancelled&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;Any&amp;nbsp;help?&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2023 07:35:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-values-stored-in-different-variables-are-not-equal-then/m-p/884444#M349413</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2023-07-12T07:35:51Z</dc:date>
    </item>
    <item>
      <title>Re: if values stored in different variables are not equal then output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-values-stored-in-different-variables-are-not-equal-then/m-p/884445#M349414</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose
  data=have (where=(find(treatment,"chemo") &amp;gt; 0))
  out=want (drop=_name_ rename=(col1=visit))
;
by treatment;
var visit:;
run;

proc sort data=want nodupkey;
by treatment visit;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Jul 2023 07:42:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-values-stored-in-different-variables-are-not-equal-then/m-p/884445#M349414</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-07-12T07:42:35Z</dc:date>
    </item>
    <item>
      <title>Re: if values stored in different variables are not equal then output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-values-stored-in-different-variables-are-not-equal-then/m-p/884448#M349417</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;: thanks for the quick reply but that is not&amp;nbsp; what I really want. The result should look somehow like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	infile datalines;
	input Treatment $5. status $15.;
	datalines;
chemo completed
chemo ongoing
chemo completed
chemo cancelled
chemo completed
chemo starting soon
chemo compledted
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2023 08:00:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-values-stored-in-different-variables-are-not-equal-then/m-p/884448#M349417</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2023-07-12T08:00:38Z</dc:date>
    </item>
    <item>
      <title>Re: if values stored in different variables are not equal then output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-values-stored-in-different-variables-are-not-equal-then/m-p/884454#M349422</link>
      <description>&lt;P&gt;Like that?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dsd dlm=',';
  input Treatment :$10. Visit1 :$15. Visit2 :$15. Visit3 :$15.;
  datalines;
chemo,completed,completed,completed
chemo,ongoing,completed,cancelled
chemo,completed,starting soon,completed
;

data want;
  set have;
  length Visit $15;
  if treatment='chemo';
  visit=visit1;
  output;
  if visit ne visit2 then
    do;
      visit=visit2;
      output;
    end;
  if visit ne visit3 then
    do;
      visit=visit3;
      output;
    end;
  keep Treatment Visit;
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-1689159804835.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/85730i8E6AC0ABDE61BCB7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1689159804835.png" alt="Patrick_0-1689159804835.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2023 11:03:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-values-stored-in-different-variables-are-not-equal-then/m-p/884454#M349422</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-07-12T11:03:30Z</dc:date>
    </item>
    <item>
      <title>Re: if values stored in different variables are not equal then output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-values-stored-in-different-variables-are-not-equal-then/m-p/884460#M349426</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
Assuming I understood what you mean.
*/
data have;
 infile datalines;
 input Treatment $10. Visit1 $10. Visit2 $14. Visit3 $15.;
 datalines;
chemo   completed completed     completed
Immune   ongoing ongoing       ongoing
Stem cell cancelled cancelled     cancelled
chemo   ongoing completed     cancelled
Targeted  ongoing starting soon ongoing
Brachy   cancelled cancelled   cancelled
surgery   cancelled ongoing       starting soon
chemo   completed starting soon completed
;
run;

data want;
 if _n_=1 then do;
  length k status $ 200;
  call missing(k);
  declare hash h();
  h.definekey('k');
  h.definedone();
 end;
set have(where=(Treatment='chemo'));
array x{*} $ Visit1-Visit3;
h.clear();
do i=1 to dim(x);
  if not missing(x{i}) then do;k=x{i};h.ref();end;
end;
if h.num_items=1 then do;status=coalescec(of x{*});output;end;
if h.num_items&amp;gt;1 then do;
 do i=1 to dim(x);
   status=x{i};
   if not missing(status) then output;
 end;
end;
keep Treatment status;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Jul 2023 11:37:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-values-stored-in-different-variables-are-not-equal-then/m-p/884460#M349426</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-07-12T11:37:57Z</dc:date>
    </item>
    <item>
      <title>Re: if values stored in different variables are not equal then output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-values-stored-in-different-variables-are-not-equal-then/m-p/884478#M349430</link>
      <description>&lt;P&gt;So you just want to output the DISTINCT values of the multiple VISIT variables?&lt;/P&gt;
&lt;P&gt;Why not just transpose the data so there is only ONE variable to hold the VISIT information?&lt;/P&gt;
&lt;P&gt;Then you can just use PROC SORT with NODUPKEY to reduce it to just the set of distinct values.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2023 13:51:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-values-stored-in-different-variables-are-not-equal-then/m-p/884478#M349430</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-12T13:51:51Z</dc:date>
    </item>
  </channel>
</rss>

