<?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: how to repeat information from previous line in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-repeat-information-from-previous-line/m-p/907449#M358193</link>
    <description>&lt;P&gt;For given dataset and output (without explanation of requirements)&amp;nbsp; it would be something like this:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cname $ id $ assets rev sales emp code1 $ code2 $;
format _numeric_ 16.;
datalines;
Company1 CN9360002629 4196808624000 2606362179000 2606362179000 908500 4911 .
. . . . . . 4931 .
Company2 CN9375780665 101027000 1477554657000 1477554657000 80 7372 .
Company3 AU092210127 . . . . 5172 4953
Company4 BE0840549639 130972000000 64776000000 41034000000 34 1521 1521
. . . . . . 1522 1522
. . . . . . 1531 1531
. . . . . . 1541 1541
. . . . . . 6712 6500
. . . . . . 6719 7389
. . . . . . 6722 8740
;
proc print;run; 

data temp;
	set have;
	length _cname $15;
	retain _cname;
	if cname ne '' then _cname=cname;
	if cname eq '' then cname=_cname;
	drop _cname; 
run; 

data want;
	set temp;
	by cname;
	length _id _code1 _code2 $15 _assets _rev _sales _emp 8;
	retain  _id _code1 _code2 _assets _rev _sales _emp; 
	if not last.cname then do;
		if id ne '' then _id=id;
		if code1 ne '' then _code1=code1;
		if code2 ne '' then _code2=code2;
		if assets ne . then _assets=assets;
		if rev ne . then _rev=rev;
		if sales ne . then _sales=sales;
		if emp ne . then _emp=emp;
	end; 
	if not first.cname then do;	
		if id eq '' then id= _id;
		if code1 eq '' then code1=_code1;
		if code2 eq '' then code2=_code2;
		if assets eq . then assets=_assets;
		if rev eq . then rev=_rev;
		if sales eq . then sales=_sales;
		if emp eq . then emp=_emp;
	end; 
	drop _:;
proc print; run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 667px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/91197i84AC4DD1653ADBBC/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;First, CNAME is being carried forward for missing observations . Then data is being processed in BY-Groups (by CNAME)&amp;nbsp; and missing observations populated using RETAIN statement.&amp;nbsp; If there are more variables ARRAY would be a more efficient way in terms of coding.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 12 Dec 2023 00:27:23 GMT</pubDate>
    <dc:creator>A_Kh</dc:creator>
    <dc:date>2023-12-12T00:27:23Z</dc:date>
    <item>
      <title>how to repeat information from previous line</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-repeat-information-from-previous-line/m-p/907349#M358173</link>
      <description>&lt;P&gt;This is the data I have:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cname $ id $ assets rev sales emp code1 $ code2 $;
datalines;
Company1 CN9360002629 4196808624000 2606362179000 2606362179000 908500 4911 .
. . . . . . 4931 .
Company2 CN9375780665 101027000 1477554657000 1477554657000 80 7372 .
Company3 AU092210127 . . . . 5172 4953
Company4 BE0840549639 130972000000 64776000000 41034000000 34 1521 1521
. . . . . . 1522 1522
. . . . . . 1531 1531
. . . . . . 1541 1541
. . . . . . 6712 6500
. . . . . . 6719 7389
. . . . . . 6722 8740
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The lines that do not have the variables cname, id have data that belong to the previous cname and id. I would like to add the data that is in the previous line.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I want:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input cname $ id $ assets rev sales emp code1 $ code2 $;
datalines;
Company1 CN9360002629 4196808624000 2606362179000 2606362179000 908500 4911 .
Company1 CN9360002629 4196808624000 2606362179000 2606362179000 908500 4931 .
Company2 CN9375780665 101027000 1477554657000 1477554657000 80 7372 .
Company3 AU092210127 . . . . 5172 4953
Company4 BE0840549639 130972000000 64776000000 41034000000 34 1521 1521
Company4 BE0840549639 130972000000 64776000000 41034000000 34 1522 1522
Company4 BE0840549639 130972000000 64776000000 41034000000 34 1531 1531
Company4 BE0840549639 130972000000 64776000000 41034000000 34 1541 1541
Company4 BE0840549639 130972000000 64776000000 41034000000 34 6712 6500
Company4 BE0840549639 130972000000 64776000000 41034000000 34 6719 7389
Company4 BE0840549639 130972000000 64776000000 41034000000 34 6722 8740
;
&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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Dec 2023 16:32:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-repeat-information-from-previous-line/m-p/907349#M358173</guid>
      <dc:creator>Satori</dc:creator>
      <dc:date>2023-12-11T16:32:40Z</dc:date>
    </item>
    <item>
      <title>Re: how to repeat information from previous line</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-repeat-information-from-previous-line/m-p/907355#M358175</link>
      <description>&lt;P&gt;RETAIN the variables.&amp;nbsp; Check the line before reading the variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input dummy $ @1 @;
  if dummy ne ' ' then input cname $ id $ assets rev sales emp code1 $ code2 $;
  else input (6*dummy code1 code2) (:$);
  drop dummy ;
  retain cname -- emp ;
datalines;
Company1 CN9360002629 4196808624000 2606362179000 2606362179000 908500 4911 .
. . . . . . 4931 .
Company2 CN9375780665 101027000 1477554657000 1477554657000 80 7372 .
Company3 AU092210127 . . . . 5172 4953
Company4 BE0840549639 130972000000 64776000000 41034000000 34 1521 1521
. . . . . . 1522 1522
. . . . . . 1531 1531
. . . . . . 1541 1541
. . . . . . 6712 6500
. . . . . . 6719 7389
. . . . . . 6722 8740
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you unfortunately already have the messed up data as a dataset you could treat it as a last observations carried forward problem.&amp;nbsp; But you really need a grouping variable for that.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data groups;
  retain group;
  set have;
  group + (cname ne ' ') ;
run;

data want;
  update groups(obs=0) groups;
  by group;
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    group     cname         id             assets            rev          sales       emp    code1    code2

  1      1      Company1    CN936000    4.1968086E12    2.606362E12    2.606362E12    908500    4911
  2      1      Company1    CN936000    4.1968086E12    2.606362E12    2.606362E12    908500    4931
  3      2      Company2    CN937578       101027000    1.477555E12    1.477555E12        80    7372
  4      3      Company3    AU092210               .              .              .         .    5172     4953
  5      4      Company4    BE084054    130972000000    64776000000    41034000000        34    1521     1521
  6      4      Company4    BE084054    130972000000    64776000000    41034000000        34    1522     1522
  7      4      Company4    BE084054    130972000000    64776000000    41034000000        34    1531     1531
  8      4      Company4    BE084054    130972000000    64776000000    41034000000        34    1541     1541
  9      4      Company4    BE084054    130972000000    64776000000    41034000000        34    6712     6500
 10      4      Company4    BE084054    130972000000    64776000000    41034000000        34    6719     7389
 11      4      Company4    BE084054    130972000000    64776000000    41034000000        34    6722     8740
&lt;/PRE&gt;
&lt;P&gt;Id you don't want the missing values of CODE1 and CODE2 filled in with the previous value then add this statement after the BY statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set groups(keep=code1 code2);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2023 01:08:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-repeat-information-from-previous-line/m-p/907355#M358175</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-12-12T01:08:39Z</dc:date>
    </item>
    <item>
      <title>Re: how to repeat information from previous line</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-repeat-information-from-previous-line/m-p/907367#M358177</link>
      <description>How can I do this for a dataset that already exists? Can I use this in a data step?</description>
      <pubDate>Mon, 11 Dec 2023 18:20:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-repeat-information-from-previous-line/m-p/907367#M358177</guid>
      <dc:creator>Satori</dc:creator>
      <dc:date>2023-12-11T18:20:52Z</dc:date>
    </item>
    <item>
      <title>Re: how to repeat information from previous line</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-repeat-information-from-previous-line/m-p/907449#M358193</link>
      <description>&lt;P&gt;For given dataset and output (without explanation of requirements)&amp;nbsp; it would be something like this:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cname $ id $ assets rev sales emp code1 $ code2 $;
format _numeric_ 16.;
datalines;
Company1 CN9360002629 4196808624000 2606362179000 2606362179000 908500 4911 .
. . . . . . 4931 .
Company2 CN9375780665 101027000 1477554657000 1477554657000 80 7372 .
Company3 AU092210127 . . . . 5172 4953
Company4 BE0840549639 130972000000 64776000000 41034000000 34 1521 1521
. . . . . . 1522 1522
. . . . . . 1531 1531
. . . . . . 1541 1541
. . . . . . 6712 6500
. . . . . . 6719 7389
. . . . . . 6722 8740
;
proc print;run; 

data temp;
	set have;
	length _cname $15;
	retain _cname;
	if cname ne '' then _cname=cname;
	if cname eq '' then cname=_cname;
	drop _cname; 
run; 

data want;
	set temp;
	by cname;
	length _id _code1 _code2 $15 _assets _rev _sales _emp 8;
	retain  _id _code1 _code2 _assets _rev _sales _emp; 
	if not last.cname then do;
		if id ne '' then _id=id;
		if code1 ne '' then _code1=code1;
		if code2 ne '' then _code2=code2;
		if assets ne . then _assets=assets;
		if rev ne . then _rev=rev;
		if sales ne . then _sales=sales;
		if emp ne . then _emp=emp;
	end; 
	if not first.cname then do;	
		if id eq '' then id= _id;
		if code1 eq '' then code1=_code1;
		if code2 eq '' then code2=_code2;
		if assets eq . then assets=_assets;
		if rev eq . then rev=_rev;
		if sales eq . then sales=_sales;
		if emp eq . then emp=_emp;
	end; 
	drop _:;
proc print; run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 667px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/91197i84AC4DD1653ADBBC/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;First, CNAME is being carried forward for missing observations . Then data is being processed in BY-Groups (by CNAME)&amp;nbsp; and missing observations populated using RETAIN statement.&amp;nbsp; If there are more variables ARRAY would be a more efficient way in terms of coding.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2023 00:27:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-repeat-information-from-previous-line/m-p/907449#M358193</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-12-12T00:27:23Z</dc:date>
    </item>
    <item>
      <title>Re: how to repeat information from previous line</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-repeat-information-from-previous-line/m-p/907452#M358195</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/173636"&gt;@Satori&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;How can I do this for a dataset that already exists? Can I use this in a data step?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;See updated answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have the data as text, like your posted example, then you can use a data step that checks the line and only reads the values when they exists and remembers (RETAINs) them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise I showed how you can use the&amp;nbsp;UPDATE statement to handle last observation carried forward.&amp;nbsp; But that requires first calculating a grouping variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise you will need to make a series of NEW variables that you can RETAIN to remember the value from the earlier observations.&amp;nbsp; Here is a pattern.&amp;nbsp; Fill in the other variables fallowing the pattern.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have;
  if not missing(cname) then do;
     _cname=cname;
     _id = id;
    ...
  end;
  else do;
     cname=_cname;
     id=_id;
     ....
  end;
  retain _cname _id ...;
  drop _cname _id ...;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2023 01:06:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-repeat-information-from-previous-line/m-p/907452#M358195</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-12-12T01:06:21Z</dc:date>
    </item>
  </channel>
</rss>

