<?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: What is wrong with my retain code? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/What-is-wrong-with-my-retain-code/m-p/797254#M255912</link>
    <description>&lt;P&gt;The IF 0 THEN statement is, as I said, a lazy way of defining the variables mentioned in the KEEP= option. The data are never read, but the variables still get defined.&lt;/P&gt;</description>
    <pubDate>Fri, 18 Feb 2022 15:54:01 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2022-02-18T15:54:01Z</dc:date>
    <item>
      <title>What is wrong with my retain code?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-wrong-with-my-retain-code/m-p/795388#M255124</link>
      <description>&lt;P&gt;My data looks like this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68345i05C2A5ADDCDF4B54/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I'm trying to make one observation for each by group with the values from the variables that are not missing. For instance the observation for the first group should be:&lt;/P&gt;&lt;P&gt;col0001 col0002 col0003 col0004 col0005 col0006 col0007 col0008 col0009 col0010 col0011 col0012&lt;BR /&gt;ID001002 Event1 03MAY2021 (54) 1 No No Yes No test 14 13&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data support;
	set data;
	by col0001 col0002 col0003 col0004;

	retain col_0006 col_0007 col_0008 col_0009 col_0010 col_0011 col_0012 col_0013 col_0014;

	if first.col0004 then
	do;
		col_0006="";
		col_0007="";
		col_0008="";
		col_0009="";
		col_0010="";
		col_0011="";
		col_0012="";
		col_0013="";
		col_0014="";
	end;

	if not missing (col0006) then col_0006= col0006;
	if not missing (col0007) then col_0007= col0007;
	if not missing (col0008) then col_0008= col0008;
	if not missing (col0009) then col_0009= col0009;
	if not missing (col0010) then col_0010= col0010;
	if not missing (col0011) then col_0011= col0011;
	if not missing (col0012) then col_0012= col0012;
	if not missing (col0013) then col_0013= col0013;
	if not missing (col0014) then col_0014= col0014;	

	if last.col0004 then
	output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;And this is the output:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68346i1AB4E828A82E7B7B/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What am I doing wrong?&lt;/P&gt;</description>
      <pubDate>Wed, 09 Feb 2022 23:23:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-wrong-with-my-retain-code/m-p/795388#M255124</guid>
      <dc:creator>Datino</dc:creator>
      <dc:date>2022-02-09T23:23:10Z</dc:date>
    </item>
    <item>
      <title>Re: What is wrong with my retain code?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-wrong-with-my-retain-code/m-p/795391#M255127</link>
      <description>&lt;P&gt;I can't comment on the code, besides requesting the log as the code looks correct. My next guess would be that those cells that look missing may not be - issue with character fields sometimes. Look at the COMPRESS() function with the s modifier to remove those 'invisible blanks'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, if this is your goal:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;P&gt;I'm trying to make one observation for each by group with the values from the variables that are not missing.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Does something like this work though, SQL functions work on character columns so it's a nice lazy way to avoid this.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select col0001, col0002, col0003, col0004, max(col0005) as col0005, max(col0006) as col0006
from have
group by 1, 2, 3, 4;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2;
update have(obs=0) have;
by col001 col002 col003 col004;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/123840"&gt;@Datino&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;My data looks like this:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68345i05C2A5ADDCDF4B54/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I'm trying to make one observation for each by group with the values from the variables that are not missing. For instance the observation for the first group should be:&lt;/P&gt;
&lt;P&gt;col0001 col0002 col0003 col0004 col0005 col0006 col0007 col0008 col0009 col0010 col0011 col0012&lt;BR /&gt;ID001002 Event1 03MAY2021 (54) 1 No No Yes No test 14 13&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is my code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;data support;
	set data;
	by col0001 col0002 col0003 col0004;

	retain col_0006 col_0007 col_0008 col_0009 col_0010 col_0011 col_0012 col_0013 col_0014;

	if first.col0004 then
	do;
		col_0006="";
		col_0007="";
		col_0008="";
		col_0009="";
		col_0010="";
		col_0011="";
		col_0012="";
		col_0013="";
		col_0014="";
	end;

	if not missing (col0006) then col_0006= col0006;
	if not missing (col0007) then col_0007= col0007;
	if not missing (col0008) then col_0008= col0008;
	if not missing (col0009) then col_0009= col0009;
	if not missing (col0010) then col_0010= col0010;
	if not missing (col0011) then col_0011= col0011;
	if not missing (col0012) then col_0012= col0012;
	if not missing (col0013) then col_0013= col0013;
	if not missing (col0014) then col_0014= col0014;	

	if last.col0004 then
	output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And this is the output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68346i1AB4E828A82E7B7B/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What am I doing wrong?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Feb 2022 23:46:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-wrong-with-my-retain-code/m-p/795391#M255127</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-02-09T23:46:19Z</dc:date>
    </item>
    <item>
      <title>Re: What is wrong with my retain code?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-wrong-with-my-retain-code/m-p/795405#M255136</link>
      <description>&lt;P&gt;Your source data set, at least in the first pictures has variables that are on your RETAIN statement. Example Col_0006.&lt;/P&gt;
&lt;P&gt;As such every time the data vector is read the value read from the existing data replaces the "retained" value.&lt;/P&gt;
&lt;P&gt;So you don't get to change any of the retained variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe this small example will demonstrate;&lt;/P&gt;
&lt;PRE&gt;data have;
  input x;
datalines;
1
2
3
;

data want;
   set have;
   retain x newx;
   newx= x;
   x= x+6;
run;&lt;/PRE&gt;
&lt;P&gt;You can see that Newx has the value from the Have dataset, indicating the value of X is reset when the next record is read.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Feb 2022 01:12:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-wrong-with-my-retain-code/m-p/795405#M255136</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-02-10T01:12:01Z</dc:date>
    </item>
    <item>
      <title>Re: What is wrong with my retain code?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-wrong-with-my-retain-code/m-p/795491#M255170</link>
      <description>&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;remarked, the variables that you retain are also on the input data. To make your code work, you can do something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data support;
  set data(drop=col_:);
  by col0001 col0002 col0003 col0004;
  if 0 then set data(keep=col_:);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And then the rest of your code unchanged. This way, the lengths of the COL_ variables are set by the second SET statement (which is never executed), just a lazy way of defining them without actually reading them.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Feb 2022 15:14:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-wrong-with-my-retain-code/m-p/795491#M255170</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-02-10T15:14:22Z</dc:date>
    </item>
    <item>
      <title>Re: What is wrong with my retain code?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-wrong-with-my-retain-code/m-p/795586#M255210</link>
      <description>&lt;P&gt;So, the solution is to create new variables that are not in the source data set?&lt;/P&gt;</description>
      <pubDate>Fri, 11 Feb 2022 02:43:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-wrong-with-my-retain-code/m-p/795586#M255210</guid>
      <dc:creator>Datino</dc:creator>
      <dc:date>2022-02-11T02:43:18Z</dc:date>
    </item>
    <item>
      <title>Re: What is wrong with my retain code?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-wrong-with-my-retain-code/m-p/795587#M255211</link>
      <description>&lt;P&gt;Thanks, your code worked very well, although I'm not completely sure I&amp;nbsp; understand what the if statement does.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt; if 0 then set data(keep=col_:);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Feb 2022 02:45:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-wrong-with-my-retain-code/m-p/795587#M255211</guid>
      <dc:creator>Datino</dc:creator>
      <dc:date>2022-02-11T02:45:43Z</dc:date>
    </item>
    <item>
      <title>Re: What is wrong with my retain code?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-wrong-with-my-retain-code/m-p/795588#M255212</link>
      <description>&lt;P&gt;Thanks. I'm trying to avoid using sql as a learning exercise.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Feb 2022 02:46:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-wrong-with-my-retain-code/m-p/795588#M255212</guid>
      <dc:creator>Datino</dc:creator>
      <dc:date>2022-02-11T02:46:49Z</dc:date>
    </item>
    <item>
      <title>Re: What is wrong with my retain code?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-wrong-with-my-retain-code/m-p/797254#M255912</link>
      <description>&lt;P&gt;The IF 0 THEN statement is, as I said, a lazy way of defining the variables mentioned in the KEEP= option. The data are never read, but the variables still get defined.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Feb 2022 15:54:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-wrong-with-my-retain-code/m-p/797254#M255912</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-02-18T15:54:01Z</dc:date>
    </item>
  </channel>
</rss>

