<?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: Populate data values using values of other rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Populate-data-values-using-values-of-other-rows/m-p/875082#M345754</link>
    <description>Thanks a lot! &lt;BR /&gt;&lt;BR /&gt;The code works for the dummy data. What if there are many other fields with missing values but other fields do not need to be populated for their missing values in the same way? For example, if I only wanted to populate var1 but not var2, can we specify fields to be populated? Thanks again!</description>
    <pubDate>Wed, 10 May 2023 20:50:35 GMT</pubDate>
    <dc:creator>lichee</dc:creator>
    <dc:date>2023-05-10T20:50:35Z</dc:date>
    <item>
      <title>Populate data values using values of other rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Populate-data-values-using-values-of-other-rows/m-p/875063#M345745</link>
      <description>&lt;P&gt;I'm trying to replace missing values with the values of the previous row before the row with missing. The example data is as below: the data set "have" is the data I have, and "want" is the target data I want to generate. The idea is that when the fields ID and var1 have missing values, the row with missing takes the value from the row with value before the row with missing value.&lt;/P&gt;
&lt;P&gt;Thank you very much!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;infile datalines truncover dsd;&lt;BR /&gt;input Person_ID var1 $ var2 $;&lt;BR /&gt;datalines;&lt;BR /&gt;1,aa,aa1&lt;BR /&gt;,,aa2&lt;BR /&gt;2,bb,bb&lt;BR /&gt;3,cc,cc&lt;BR /&gt;4,dd,dd&lt;BR /&gt;5,ee,ee&lt;BR /&gt;6,ff,ff&lt;BR /&gt;7,gg,gg1&lt;BR /&gt;,,gg2&lt;BR /&gt;,,gg3&lt;BR /&gt;8,hh,hh1&lt;BR /&gt;,,hh2&lt;BR /&gt;,,hh3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;infile datalines truncover dsd;&lt;BR /&gt;input Person_ID var1 $ var2 $;&lt;BR /&gt;datalines;&lt;BR /&gt;1,aa,aa1&lt;BR /&gt;1,aa,aa2&lt;BR /&gt;2,bb,bb&lt;BR /&gt;3,cc,cc&lt;BR /&gt;4,dd,dd&lt;BR /&gt;5,ee,ee&lt;BR /&gt;6,ff,ff&lt;BR /&gt;7,gg,gg1&lt;BR /&gt;7,gg,gg2&lt;BR /&gt;7,gg,gg3&lt;BR /&gt;8,hh,hh1&lt;BR /&gt;8,hh,hh2&lt;BR /&gt;8,hh,hh3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 May 2023 19:18:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Populate-data-values-using-values-of-other-rows/m-p/875063#M345745</guid>
      <dc:creator>lichee</dc:creator>
      <dc:date>2023-05-10T19:18:02Z</dc:date>
    </item>
    <item>
      <title>Re: Populate data values using values of other rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Populate-data-values-using-values-of-other-rows/m-p/875066#M345747</link>
      <description>&lt;P&gt;Try the update trick.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines truncover dsd;
	input Person_ID var1 $ var2 $;
	datalines;
1,aa,aa1
,,aa2
2,bb,bb
3,cc,cc
4,dd,dd
5,ee,ee
6,ff,ff
7,gg,gg1
,,gg2
,,gg3
8,hh,hh1
,,hh2
,,hh3
;
run;

data have2;
   retain dummy  1;
   set have;
   run;

data want;
   update have2(obs=0) have2;
   by dummy;
   output;
   drop dummy;
   run;
proc print; run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 May 2023 19:27:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Populate-data-values-using-values-of-other-rows/m-p/875066#M345747</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2023-05-10T19:27:21Z</dc:date>
    </item>
    <item>
      <title>Re: Populate data values using values of other rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Populate-data-values-using-values-of-other-rows/m-p/875082#M345754</link>
      <description>Thanks a lot! &lt;BR /&gt;&lt;BR /&gt;The code works for the dummy data. What if there are many other fields with missing values but other fields do not need to be populated for their missing values in the same way? For example, if I only wanted to populate var1 but not var2, can we specify fields to be populated? Thanks again!</description>
      <pubDate>Wed, 10 May 2023 20:50:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Populate-data-values-using-values-of-other-rows/m-p/875082#M345754</guid>
      <dc:creator>lichee</dc:creator>
      <dc:date>2023-05-10T20:50:35Z</dc:date>
    </item>
    <item>
      <title>Re: Populate data values using values of other rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Populate-data-values-using-values-of-other-rows/m-p/875098#M345767</link>
      <description>&lt;P&gt;Use the UPDATE/BY statements and keep (data set options) only the variables that you want to carry forward.&amp;nbsp; Then use SET dropping the variables you kept in the UPDATE statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you supply some sample data I will make example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0 then set have2;
update have2(keep=dummy obs=0) have2(keep=dummy x y z);
by dummy;
set have2(drop=dummy x y z);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 May 2023 21:57:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Populate-data-values-using-values-of-other-rows/m-p/875098#M345767</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2023-05-10T21:57:43Z</dc:date>
    </item>
    <item>
      <title>Re: Populate data values using values of other rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Populate-data-values-using-values-of-other-rows/m-p/875105#M345769</link>
      <description>&lt;P&gt;Here is the solution to retain values on multiple variables and assign the values on the basis of different conditions. The variables may have different or the same conditions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this example I've used following conditions:&amp;nbsp;&amp;nbsp;&lt;CODE class=" language-sas"&gt;Person_ID eq .&lt;/CODE&gt;&amp;nbsp; &amp;nbsp;&lt;CODE class=" language-sas"&gt;var1&amp;nbsp;eq''&lt;/CODE&gt;&amp;nbsp;&amp;nbsp;&lt;CODE class=" language-sas"&gt;var2 eq '' &lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;But it can be easily changed and adjusted according to your requirement.&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15410"&gt;@data_null__&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines truncover dsd;
	input Person_ID var1 $ var2 $;
	datalines;
1,aa,aa1
,,aa2
2,bb,bb
3,cc,cc
4,dd,dd
5,ee,ee
6,ff,ff
7,gg,gg1
,,gg2
,,gg3
8,hh,hh1
,,hh2
,,hh3
;
run;
proc print;

data want;
    set have;
    retain temp1 temp2 temp3;
    if _N_=1 then do;
    	temp1=Person_ID; 
    	temp2=var1; 
    	temp3=var2; 
    	output; 
    	end;
    else do; 
    	/* for Person_ID */
    	if Person_ID eq . then do; 
    		temp1=temp1; 
    		Person_ID=temp1; 
    		end;
    	else do; 
    		temp1=Person_ID; 
    		Person_ID=Person_ID; 
    		end;
     	
     	/* for var1 */
    	if var1 eq '' then do; 
    		temp2=temp2; 
    		var1=temp2; 
    		end;
    	else do; 
    		temp2=var1; 
    		var1=var1; 
    		end;
     	
     	/* for var2 */
    	if var2 eq '' then do; 
    		temp3=temp3; 
    		var2=temp3; 
    		end;
    	else do; 
    		temp3=var2; 
    		var2=var2; 
    		end;
     	
     output;
     temp1=Person_ID;
     temp2=var1;
     temp3=var2;
     
     end;
   drop temp1 temp2 temp3;
 run;
 proc print;
    &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MayurJadhav_0-1683757444301.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83848i967034626C95AF8B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MayurJadhav_0-1683757444301.png" alt="MayurJadhav_0-1683757444301.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 May 2023 22:24:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Populate-data-values-using-values-of-other-rows/m-p/875105#M345769</guid>
      <dc:creator>MayurJadhav</dc:creator>
      <dc:date>2023-05-10T22:24:27Z</dc:date>
    </item>
    <item>
      <title>Re: Populate data values using values of other rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Populate-data-values-using-values-of-other-rows/m-p/875130#M345778</link>
      <description>&lt;P&gt;The dummy data would look like below. Basically only Person_ID and var1 need to be populated (as from data have to data want), but not var3 or var4 even thought they have missing values as well.&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;infile datalines truncover dsd;&lt;BR /&gt;input Person_ID var1 $ var2 $ var3 $ var4;&lt;BR /&gt;datalines;&lt;BR /&gt;ID,var1,var2,var3,var4&lt;BR /&gt;1,aa,aa1,aaa,&lt;BR /&gt;,,aa2,,10&lt;BR /&gt;2,bb,bb,bbb,6&lt;BR /&gt;3,cc,cc,ccc,8&lt;BR /&gt;4,dd,dd,ddd,&lt;BR /&gt;5,ee,ee,eee,7&lt;BR /&gt;6,ff,ff,fff,&lt;BR /&gt;7,gg,gg1,ggg,3&lt;BR /&gt;,,gg2,,5&lt;BR /&gt;,,gg3,,6&lt;BR /&gt;8,hh,hh1,hhh,&lt;BR /&gt;,,hh2,,&lt;BR /&gt;,,hh3,,&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;infile datalines truncover dsd;&lt;BR /&gt;input Person_ID var1 $ var2 $ var3 $ var4;&lt;BR /&gt;datalines;&lt;BR /&gt;1,aa,aa1,aaa,&lt;BR /&gt;1,aa,aa2,,10&lt;BR /&gt;2,bb,bb,bbb,6&lt;BR /&gt;3,cc,cc,ccc,8&lt;BR /&gt;4,dd,dd,ddd,&lt;BR /&gt;5,ee,ee,eee,7&lt;BR /&gt;6,ff,ff,fff,&lt;BR /&gt;7,gg,gg1,ggg,3&lt;BR /&gt;7,gg,gg2,,5&lt;BR /&gt;7,gg,gg3,,6&lt;BR /&gt;8,hh,hh1,hhh,&lt;BR /&gt;8,hh,hh2,,&lt;BR /&gt;8,hh,hh3,,&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 02:33:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Populate-data-values-using-values-of-other-rows/m-p/875130#M345778</guid>
      <dc:creator>lichee</dc:creator>
      <dc:date>2023-05-11T02:33:08Z</dc:date>
    </item>
    <item>
      <title>Re: Populate data values using values of other rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Populate-data-values-using-values-of-other-rows/m-p/875131#M345779</link>
      <description>&lt;P&gt;Sorry! Data have should be as below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;infile datalines truncover dsd;&lt;BR /&gt;input Person_ID var1 $ var2 $ var3 $ var4;&lt;BR /&gt;datalines;&lt;BR /&gt;1,aa,aa1,aaa,&lt;BR /&gt;,,aa2,,10&lt;BR /&gt;2,bb,bb,bbb,6&lt;BR /&gt;3,cc,cc,ccc,8&lt;BR /&gt;4,dd,dd,ddd,&lt;BR /&gt;5,ee,ee,eee,7&lt;BR /&gt;6,ff,ff,fff,&lt;BR /&gt;7,gg,gg1,ggg,3&lt;BR /&gt;,,gg2,,5&lt;BR /&gt;,,gg3,,6&lt;BR /&gt;8,hh,hh1,hhh,&lt;BR /&gt;,,hh2,,&lt;BR /&gt;,,hh3,,&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 02:35:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Populate-data-values-using-values-of-other-rows/m-p/875131#M345779</guid>
      <dc:creator>lichee</dc:creator>
      <dc:date>2023-05-11T02:35:05Z</dc:date>
    </item>
    <item>
      <title>Re: Populate data values using values of other rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Populate-data-values-using-values-of-other-rows/m-p/875176#M345798</link>
      <description>&lt;P&gt;This method should work! Looking at your input dataset you just need to add&amp;nbsp;&lt;SPAN&gt;var4 with condition:&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;if var4 eq . then do&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's a bit long code but simple and easy to understand. It would be possible to further optimize this code, possibly using ARRAY or other sas procedures.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But this method is good enough to achieve desired output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/430334"&gt;@lichee&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 10:59:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Populate-data-values-using-values-of-other-rows/m-p/875176#M345798</guid>
      <dc:creator>MayurJadhav</dc:creator>
      <dc:date>2023-05-11T10:59:53Z</dc:date>
    </item>
    <item>
      <title>Re: Populate data values using values of other rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Populate-data-values-using-values-of-other-rows/m-p/875197#M345810</link>
      <description>&lt;P&gt;Here you go.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover dsd;
input Person_ID var1 $ var2 $ var3 $ var4;
datalines;
1,aa,aa1,aaa,
,,aa2,,10
2,bb,bb,bbb,6
3,cc,cc,ccc,8
4,dd,dd,ddd,
5,ee,ee,eee,7
6,ff,ff,fff,
7,gg,gg1,ggg,3
,,gg2,,5
,,gg3,,6
8,hh,hh1,hhh,
,,hh2,,
,,hh3,,
;
run;

data have2;
   retain dummy 1;
   set have;
   run;
proc print; run;
data want;
   if 0 then set have2;
   update have2(keep=dummy obs=0) have2(keep=dummy Person_id var1-var2);
   by dummy;
   set have2(keep=var3-var4);
   output;
   drop dummy;
   run;
proc print;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 330px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83859i4C390B6B551D2D29/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, 11 May 2023 13:09:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Populate-data-values-using-values-of-other-rows/m-p/875197#M345810</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2023-05-11T13:09:29Z</dc:date>
    </item>
  </channel>
</rss>

