<?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: Copying data down a column in SAS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Copying-data-down-a-column-in-SAS/m-p/557373#M155374</link>
    <description>Hi! Thanks so much for your solution. It's actually super robust and after making changed to my dataset it still worked. Thanks!</description>
    <pubDate>Thu, 09 May 2019 07:32:06 GMT</pubDate>
    <dc:creator>serena13lee</dc:creator>
    <dc:date>2019-05-09T07:32:06Z</dc:date>
    <item>
      <title>Copying data down a column in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copying-data-down-a-column-in-SAS/m-p/557341#M155355</link>
      <description>&lt;P&gt;I have a dataset that has blanks and I would like to fill in the spaces based on other columns. HT, WT, and BI are filled in for patient and visit by variable. I would like to copy over the values of each patient and visit into the other variables. Can this be done by writing if patient and visit match, then copy value over? Or is there another way?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
input Patient $ Visit Variable $ HT WT BI; 
cards;
A 1 H 1 . .
A 2 H 3 . .
A 3 H 4 . .
A 1 W . 2 . 
A 2 W . 5 .
A 3 W . 9 .
A 1 B . . 0
A 2 B . . 8
A 3 B . . 8 
B 1 H 1 . .
B 2 H 3 . .
B 3 H 1 . .
B 1 W . 2 . 
B 2 W . 4 .
B 3 W . 1 .
B 1 B . . 0
B 2 B . . 0
B 3 B . . 8 
;&lt;BR /&gt;&lt;BR /&gt;data want; 
input Patient $ Visit Variable $ HT WT BI; 
cards;
A 1 H 1 2 0
A 2 H 3 5 0
A 3 H 4 9 8
A 1 W 1 2 0 
A 2 W 3 5 8
A 3 W 4 9 8
A 1 B 1 2 0
A 2 B 3 5 8
A 3 B 4 9 8 
B 1 H 1 2 0
B 2 H 3 4 0
B 3 H 1 1 8
B 1 W 1 2 0 
B 2 W 3 4 0
B 3 W 1 1 8
B 1 B 1 2 0
B 2 B 3 4 0
B 3 B 1 1 8 
;&lt;BR /&gt;&lt;BR /&gt;Initially&amp;nbsp;I&amp;nbsp;thought&amp;nbsp;this&amp;nbsp;could&amp;nbsp;be&amp;nbsp;fixed&amp;nbsp;with&amp;nbsp;an&amp;nbsp;if-else&amp;nbsp;statement&amp;nbsp;but&amp;nbsp;this&amp;nbsp;may&amp;nbsp;be&amp;nbsp;more&amp;nbsp;complex?&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 May 2019 02:57:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copying-data-down-a-column-in-SAS/m-p/557341#M155355</guid>
      <dc:creator>serena13lee</dc:creator>
      <dc:date>2019-05-09T02:57:44Z</dc:date>
    </item>
    <item>
      <title>Re: Copying data down a column in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copying-data-down-a-column-in-SAS/m-p/557348#M155358</link>
      <description>&lt;P&gt;It's harder than it looks.&amp;nbsp; Here's a solution that depends on VISIT actually taking on values of 1, 2, and 3 as indicated in your sample data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   array htvals {3} _temporary_;
   array wtvals {3} _temporary_;
   array bivals {3} _temporary_;
   do until (last.patient);
      set have;
      by patient;
      if ht &amp;gt; . then htvals{visit} = ht;
      if wt &amp;gt; . then wtvals{visit} = wt;
      if bi &amp;gt; . then bivals{visit} = bi;
   end;
   do until (last.patient);
      set have;
      by patient;
      if ht = . then ht = htvals{visit};
      if wt = . then wt = wtvals{visit};
      if bi = . then bi = bivals{visit};
      output;
   end;
   do _n_=1 to 3;
      htvals{_n_} = .;
      wtvals{_n_} = .;
      bivals{_n_} = .;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Wish I found an easier way, but this is all I came up with.&lt;/P&gt;</description>
      <pubDate>Thu, 09 May 2019 02:57:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copying-data-down-a-column-in-SAS/m-p/557348#M155358</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-05-09T02:57:25Z</dc:date>
    </item>
    <item>
      <title>Re: Copying data down a column in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copying-data-down-a-column-in-SAS/m-p/557350#M155360</link>
      <description>&lt;P&gt;Without actually knowing what you want to do with the data it seems like you have duplication of the data.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Either you should remove the variable "Variable" or you should have the variables&amp;nbsp;Patient,Visit, Variable and a new one called "Value" or similar. This is an example of the first one:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=have2; &lt;BR /&gt;	*We need to make sure that the data is sorted on patient and visit for the next step.;
	by patient visit ;
run;
data want (rename=(HT_=HT   WT_=WT    BI_=BI));
	set have2;
	by patient visit ;
	retain HT_ WT_ BI_; *We want to keep the values from the variables, but can only do this if they are not from the input data set.;

	*Put the right values in the right variable.;&lt;BR /&gt;	if variable = "H" then HT_ = HT;
	else if variable = "W" then WT_ = WT;
	else if variable = "B" then BI_ = BI;&lt;BR /&gt;&lt;BR /&gt;	*Only output the last row of each Patient and visit. ;
	if last.visit then output;&lt;BR /&gt;&lt;BR /&gt;	*Get rid of some variables. ;
	drop variable HT WT BI;
	run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This will give you the following data:&lt;/P&gt;&lt;P&gt;Patient Visit HT WT BI&lt;BR /&gt;A 1 1 2 0&lt;BR /&gt;A 2 3 5 8&lt;BR /&gt;A 3 4 9 8&lt;BR /&gt;B 1 1 2 0&lt;BR /&gt;B 2 3 4 0&lt;BR /&gt;B 3 1 1 8&lt;/P&gt;</description>
      <pubDate>Thu, 09 May 2019 03:37:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copying-data-down-a-column-in-SAS/m-p/557350#M155360</guid>
      <dc:creator>heffo</dc:creator>
      <dc:date>2019-05-09T03:37:08Z</dc:date>
    </item>
    <item>
      <title>Re: Copying data down a column in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copying-data-down-a-column-in-SAS/m-p/557364#M155370</link>
      <description>&lt;P&gt;Please try the proc sql approach&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
input Patient $ Visit Variable $ HT WT BI; 
cards;
A 1 H 1 . .
A 2 H 3 . .
A 3 H 4 . .
A 1 W . 2 . 
A 2 W . 5 .
A 3 W . 9 .
A 1 B . . 0
A 2 B . . 8
A 3 B . . 8 
B 1 H 1 . .
B 2 H 3 . .
B 3 H 1 . .
B 1 W . 2 . 
B 2 W . 4 .
B 3 W . 1 .
B 1 B . . 0
B 2 B . . 0
B 3 B . . 8 
;

proc sql;
create table want as select a.patient,a.visit,a.variable,b.ht,b.wt,b.bi from have as a inner join (select patient,visit,max(ht) as ht, max(wt) as wt, max(bi) as bi from have group by patient, visit) as b on a.patient=b.patient and a.visit=b.visit order by a.patient,a.variable,a.visit;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 May 2019 06:43:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copying-data-down-a-column-in-SAS/m-p/557364#M155370</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-05-09T06:43:38Z</dc:date>
    </item>
    <item>
      <title>Re: Copying data down a column in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copying-data-down-a-column-in-SAS/m-p/557366#M155371</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
input Patient $ Visit Variable $ HT WT BI; 
cards;
A 1 H 1 . .
A 2 H 3 . .
A 3 H 4 . .
A 1 W . 2 . 
A 2 W . 5 .
A 3 W . 9 .
A 1 B . . 0
A 2 B . . 8
A 3 B . . 8 
B 1 H 1 . .
B 2 H 3 . .
B 3 H 1 . .
B 1 W . 2 . 
B 2 W . 4 .
B 3 W . 1 .
B 1 B . . 0
B 2 B . . 0
B 3 B . . 8 
;
run;

data want; 
input Patient $ Visit Variable $ HT WT BI; 
cards;
A 1 H 1 2 0
A 2 H 3 5 0
A 3 H 4 9 8
A 1 W 1 2 0 
A 2 W 3 5 8
A 3 W 4 9 8
A 1 B 1 2 0
A 2 B 3 5 8
A 3 B 4 9 8 
B 1 H 1 2 0
B 2 H 3 4 0
B 3 H 1 1 8
B 1 W 1 2 0 
B 2 W 3 4 0
B 3 W 1 1 8
B 1 B 1 2 0
B 2 B 3 4 0
B 3 B 1 1 8 
;
run;

proc summary data=have nway;
   class Patient Visit;
   var HT WT BI;
   output out=max max=;
run;

data test;
   * set PDV order (optional) ;
   if 0 then set have;

   * initialize &amp;amp;_hashnum_ to zero then declare hash objects ;
   %let _hashnum_=0;
   %hash_define(data=max, keys=Patient Visit, vars=HT WT BI);

   set have;
   %hash_lookup;

   drop _rc:;
run;

proc compare base=want comp=test;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Do you have a typo in your want dataset?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternate approach:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
input Patient $ Visit Variable $ HT WT BI; 
cards;
A 1 H 1 . .
A 2 H 3 . .
A 3 H 4 . .
A 1 W . 2 . 
A 2 W . 5 .
A 3 W . 9 .
A 1 B . . 0
A 2 B . . 8
A 3 B . . 8 
B 1 H 1 . .
B 2 H 3 . .
B 3 H 1 . .
B 1 W . 2 . 
B 2 W . 4 .
B 3 W . 1 .
B 1 B . . 0
B 2 B . . 0
B 3 B . . 8 
;
run;

data want; 
input Patient $ Visit Variable $ HT WT BI; 
cards;
A 1 H 1 2 0
A 2 H 3 5 0
A 3 H 4 9 8
A 1 W 1 2 0 
A 2 W 3 5 8
A 3 W 4 9 8
A 1 B 1 2 0
A 2 B 3 5 8
A 3 B 4 9 8 
B 1 H 1 2 0
B 2 H 3 4 0
B 3 H 1 1 8
B 1 W 1 2 0 
B 2 W 3 4 0
B 3 W 1 1 8
B 1 B 1 2 0
B 2 B 3 4 0
B 3 B 1 1 8 
;
run;

proc summary data=have nway;
   class Patient Visit;
   var HT WT BI;
   output out=max max= / autoname;
run;

data test;
   * set PDV order (optional) ;
   if 0 then set have;

   * initialize &amp;amp;_hashnum_ to zero then declare hash objects ;
   %let _hashnum_=0;
   %hash_define(data=max, keys=Patient Visit, vars=HT_Max WT_Max BI_Max);

   set have;
   %hash_lookup;

   array vars{*} HT WT BI;
   array max{*} HT_Max WT_Max BI_Max;
   do i=1 to dim(vars);
      if missing(vars{i}) then vars{i}=max{i};
   end;

   drop _rc: i HT_Max WT_Max BI_Max;
run;

proc compare base=want comp=test;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;See&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/scottbass/SAS/blob/master/Macro/hash_define.sas" target="_blank"&gt;https://github.com/scottbass/SAS/blob/master/Macro/hash_define.sas&lt;/A&gt;&amp;nbsp;and&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/scottbass/SAS/blob/master/Macro/hash_define.sas" target="_blank"&gt;https://github.com/scottbass/SAS/blob/master/Macro/hash_lookup.sas&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your data differs from what you posted then this approach may not work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(Can you add "run;" statements to your have and want data steps as best practice?)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 May 2019 06:50:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copying-data-down-a-column-in-SAS/m-p/557366#M155371</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-05-09T06:50:02Z</dc:date>
    </item>
    <item>
      <title>Re: Copying data down a column in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copying-data-down-a-column-in-SAS/m-p/557367#M155372</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt;&amp;nbsp;'s approach is similar to mine, and I think is a clearer, cleaner approach.&lt;/P&gt;</description>
      <pubDate>Thu, 09 May 2019 06:52:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copying-data-down-a-column-in-SAS/m-p/557367#M155372</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-05-09T06:52:23Z</dc:date>
    </item>
    <item>
      <title>Re: Copying data down a column in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copying-data-down-a-column-in-SAS/m-p/557373#M155374</link>
      <description>Hi! Thanks so much for your solution. It's actually super robust and after making changed to my dataset it still worked. Thanks!</description>
      <pubDate>Thu, 09 May 2019 07:32:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copying-data-down-a-column-in-SAS/m-p/557373#M155374</guid>
      <dc:creator>serena13lee</dc:creator>
      <dc:date>2019-05-09T07:32:06Z</dc:date>
    </item>
    <item>
      <title>Re: Copying data down a column in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copying-data-down-a-column-in-SAS/m-p/557427#M155397</link>
      <description>&lt;PRE&gt;data have; 
input Patient $ Visit Variable $ HT WT BI; 
cards;
A 1 H 1 . .
A 2 H 3 . .
A 3 H 4 . .
A 1 W . 2 . 
A 2 W . 5 .
A 3 W . 9 .
A 1 B . . 0
A 2 B . . 8
A 3 B . . 8 
B 1 H 1 . .
B 2 H 3 . .
B 3 H 1 . .
B 1 W . 2 . 
B 2 W . 4 .
B 3 W . 1 .
B 1 B . . 0
B 2 B . . 0
B 3 B . . 8
;

proc sort data=have(drop= Variable ) out=temp;
 by Patient  Visit;
run;
data key;
 update temp(obs=0) temp;
 by Patient  Visit;
run;
data want;
 if _n_=1 then do;
   if 0 then set key;
   declare hash h(dataset:'key');
   h.definekey( 'Patient' ,'Visit');
   h.definedata('HT', 'WT', 'BI');
   h.definedone();
 end;
set have(drop=HT WT BI);
call missing(HT ,WT ,BI);
h.find();
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 May 2019 13:22:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copying-data-down-a-column-in-SAS/m-p/557427#M155397</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-05-09T13:22:13Z</dc:date>
    </item>
    <item>
      <title>Re: Copying data down a column in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copying-data-down-a-column-in-SAS/m-p/557503#M155421</link>
      <description>This is a super clean solution! I will try modifying it to my dataset and see how it looks. Thanks!</description>
      <pubDate>Thu, 09 May 2019 16:22:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copying-data-down-a-column-in-SAS/m-p/557503#M155421</guid>
      <dc:creator>serena13lee</dc:creator>
      <dc:date>2019-05-09T16:22:40Z</dc:date>
    </item>
  </channel>
</rss>

