<?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: Match the cell value to another cell within the table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Match-the-cell-value-to-another-cell-within-the-table/m-p/624177#M183826</link>
    <description>&lt;P&gt;First of all: bad data modeling. Transpose to long.&lt;/P&gt;
&lt;P&gt;Then build a format, apply it, and drop empty values on the way.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dsd dlm=' ' truncover;
input ID D1$ D2$ D3$ Dx1$ Dx2$ Dx3$;
cards;
123 Med1 Med4 Med3 abc . .
456 Med2 Med5 Med7 def . .
789 Med3 Med3 Med2 ghi . .
147 Med4 Med6 . jkl . .
258 Med5 Med1 Med8 mno . .
369 Med6 Med2 . pqr . .
;

proc transpose data=have out=trans;
var D:;
by id notsorted;
run;

data
  cntlin (
    drop=id _name_
    where=(label ne '')
  )
  trans2 (
    keep=id start
    rename=(start=D)
    where=(D ne '')
  )
;
merge
  trans (where=(length(_name_)=2) rename=(col1=start))
  trans (where=(length(_name_)=3) rename=(col1=label))
  end=done;
;
fmtname = 'medic';
type = 'C';
output cntlin;
output trans2;
if done
then do;
  hlo = 'O';
  label = '***';
  output cntlin;
end;
run;

proc format cntlin=cntlin;
run;

data bad;
set have;
array D D1-D3;
array dx Dx1-Dx3;
do i = 1 to dim(D);
  dx{i} = put(d{i},medic.);
end;
drop i;
run;

data good;
set trans2;
Dx = put(D,medic.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;bad is the format applied to the wide format, good is a properly modeled dataset.&lt;/P&gt;</description>
    <pubDate>Wed, 12 Feb 2020 14:11:42 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-02-12T14:11:42Z</dc:date>
    <item>
      <title>Match the cell value to another cell within the table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-the-cell-value-to-another-cell-within-the-table/m-p/624170#M183821</link>
      <description>&lt;P&gt;Dear Experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to match the value of a particular cell(in Variable1) to another cell(of Variable2) which placed in different Observation.&lt;/P&gt;&lt;P&gt;I'll share the example set to make it a clear picture.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID D1$ D2$ D3$ Dx1$ Dx2$ Dx3$;
cards;
123 Med1 Med4 Med3 abc . .
456 Med2 Med5 Med7 def . .
789 Med3 Med3 Med2 ghi . .
147 Med4 Med6 .	jkl . .
258 Med5 Med1 Med8 mno . .
369 Med6 Med2 . pqr . .
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The D1 refers to Medicine name; Dx1 refers to Generic name. I updated the Dx1 value manually. All I need to update the matched values in all the consecutive variables (Dx2, Dx3,...). Once the Dx1 and Dx2 are updated. I turn the same step for Dx3 using the Combination match of Dx1 and Dx2 = Dx3. Kindly let me know for any clarification required in this query.&lt;/P&gt;&lt;P&gt;Expected Output&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;D1&lt;/TD&gt;&lt;TD&gt;D2&lt;/TD&gt;&lt;TD&gt;D3&lt;/TD&gt;&lt;TD&gt;Dx1&lt;/TD&gt;&lt;TD&gt;Dx2&lt;/TD&gt;&lt;TD&gt;Dx3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;Med1&lt;/TD&gt;&lt;TD&gt;Med4&lt;/TD&gt;&lt;TD&gt;Med3&lt;/TD&gt;&lt;TD&gt;abc&lt;/TD&gt;&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;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Feb 2020 13:42:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-the-cell-value-to-another-cell-within-the-table/m-p/624170#M183821</guid>
      <dc:creator>Sathish_jammy</dc:creator>
      <dc:date>2020-02-12T13:42:16Z</dc:date>
    </item>
    <item>
      <title>Re: Match the cell value to another cell within the table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-the-cell-value-to-another-cell-within-the-table/m-p/624177#M183826</link>
      <description>&lt;P&gt;First of all: bad data modeling. Transpose to long.&lt;/P&gt;
&lt;P&gt;Then build a format, apply it, and drop empty values on the way.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dsd dlm=' ' truncover;
input ID D1$ D2$ D3$ Dx1$ Dx2$ Dx3$;
cards;
123 Med1 Med4 Med3 abc . .
456 Med2 Med5 Med7 def . .
789 Med3 Med3 Med2 ghi . .
147 Med4 Med6 . jkl . .
258 Med5 Med1 Med8 mno . .
369 Med6 Med2 . pqr . .
;

proc transpose data=have out=trans;
var D:;
by id notsorted;
run;

data
  cntlin (
    drop=id _name_
    where=(label ne '')
  )
  trans2 (
    keep=id start
    rename=(start=D)
    where=(D ne '')
  )
;
merge
  trans (where=(length(_name_)=2) rename=(col1=start))
  trans (where=(length(_name_)=3) rename=(col1=label))
  end=done;
;
fmtname = 'medic';
type = 'C';
output cntlin;
output trans2;
if done
then do;
  hlo = 'O';
  label = '***';
  output cntlin;
end;
run;

proc format cntlin=cntlin;
run;

data bad;
set have;
array D D1-D3;
array dx Dx1-Dx3;
do i = 1 to dim(D);
  dx{i} = put(d{i},medic.);
end;
drop i;
run;

data good;
set trans2;
Dx = put(D,medic.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;bad is the format applied to the wide format, good is a properly modeled dataset.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Feb 2020 14:11:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-the-cell-value-to-another-cell-within-the-table/m-p/624177#M183826</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-02-12T14:11:42Z</dc:date>
    </item>
    <item>
      <title>Re: Match the cell value to another cell within the table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-the-cell-value-to-another-cell-within-the-table/m-p/624179#M183828</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/215282"&gt;@Sathish_jammy&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A format will be easier for you to manage:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm=" " dsd missover;
input ID D1$ D2$ D3$ Dx1$;
cards;
123 Med1 Med4 Med3 abc
456 Med2 Med5 Med7 def
789 Med3 Med3 Med2 ghi
147 Med4 Med6  jkl
258 Med5 Med1 Med8 mno
369 Med6 Med2  pqr  
;
run;

proc format;
	value $ _Medfmt 'Med1' = 'abc'
					'Med2' = 'def'
					'Med3' = 'ghi'
					'Med4' = 'jkl'
					'Med5' = 'mno'
					'Med6' = 'pqr'
					'Med7' = 'pqr'
					other = 'NOT YET DEFINED' /*define how you want to handle those cases*/;
run;

data want;
	set have;
	Dx2 = put(D2,_Medfmt.);
	Dx3 = put(D3,_Medfmt.);
	if strip(D2) = "" then call missing(Dx2);
	if strip(D3) = "" then call missing(Dx3);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Feb 2020 14:19:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-the-cell-value-to-another-cell-within-the-table/m-p/624179#M183828</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-02-12T14:19:41Z</dc:date>
    </item>
    <item>
      <title>Re: Match the cell value to another cell within the table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-the-cell-value-to-another-cell-within-the-table/m-p/624208#M183845</link>
      <description>&lt;P&gt;I think this is a good situation to use a hash object.&amp;nbsp; In this case the hash object would be a lookup table dynamically generated from two variables in the data:&amp;nbsp; D1 and DX1, renamed to _dkey and _dx respectively.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm=" " dsd missover;
input ID D1$ D2$ D3$ Dx1$;
cards;
123 Med1 Med4 Med3 abc
456 Med2 Med5 Med7 def
789 Med3 Med3 Med2 ghi
147 Med4 Med6  jkl
258 Med5 Med1 Med8 mno
369 Med6 Med2  pqr  
;

data want (drop=_:);
  set have;
  if _n_=1 then do;
    if 0 then set have (keep=d1 dx1 rename=(d1=_dkey dx1=_dx));
    declare hash h (dataset:'have (keep=d1 dx1 rename=(d1=_dkey dx1=_dx)');
      h.definekey('_dkey');
      h.definedata(all:'Y');
      h.definedone();
  end;

  array _d      d2 d3;
  array _x  $4 dx2 dx3 ;
  do over _d;
    if not missing (_d) then do;
      _rc=h.find(key:_d);
      if _rc=0 then _x=_dx;
      call missing(_dx);
    end;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes there is a one-to-one map between D1 and DX1. &amp;nbsp; I.e., if there are multiple occurrences of a given D1, all the DX1's for those occurrences are assumed to be identical.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that is not true, then the original data pair for D1 and DX1 are kept, but the lookup object defaults to keeping only whichever D1,DX1 (renamed to _DKEY,_DX) is encountered first.&amp;nbsp; So any non one-to-many map in D1/DX1&amp;nbsp; will become one-to-one for D2/DX2&amp;nbsp; and D3/DX3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I did this using ARRAY and DO OVER incase you end up with, say, D3, D4, … &amp;nbsp; D20.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Feb 2020 15:29:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-the-cell-value-to-another-cell-within-the-table/m-p/624208#M183845</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-02-12T15:29:46Z</dc:date>
    </item>
  </channel>
</rss>

