<?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 String comparison problems with lag functions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/String-comparison-problems-with-lag-functions/m-p/246133#M46010</link>
    <description>&lt;P&gt;I'm having issues with string comparison with when it comes to lag function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Say I have the following dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA test;
	input id code $;
	cards;
	1 abc
	1 abc
	2 xyz
	2 xyy
	2 xyy
	2 zzz
	3 ccc
	3 ccc
	;
PROC SORT;
	by id code;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then I want to calculate how many distinct code values there are within each ID values, I use this program:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA test2;
	SET test;
	by id code;
	RETAIN code_cnt 1;
	ff = first.id;
	lagged = lag1(code);
	if first.id then code_cnt = 1;
	else do;
		if code ~= lag1(code) then code_cnt = code_cnt + 1;
		end;
PROC PRINT;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The resulting dataset looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Obs id code code_cnt ff lagged&lt;/P&gt;&lt;TABLE cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;abc&lt;/TD&gt;&lt;TD&gt;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ODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What is wrong?&lt;/P&gt;</description>
    <pubDate>Tue, 26 Jan 2016 16:45:06 GMT</pubDate>
    <dc:creator>CJ_Jackson</dc:creator>
    <dc:date>2016-01-26T16:45:06Z</dc:date>
    <item>
      <title>String comparison problems with lag functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-comparison-problems-with-lag-functions/m-p/246133#M46010</link>
      <description>&lt;P&gt;I'm having issues with string comparison with when it comes to lag function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Say I have the following dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA test;
	input id code $;
	cards;
	1 abc
	1 abc
	2 xyz
	2 xyy
	2 xyy
	2 zzz
	3 ccc
	3 ccc
	;
PROC SORT;
	by id code;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then I want to calculate how many distinct code values there are within each ID values, I use this program:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA test2;
	SET test;
	by id code;
	RETAIN code_cnt 1;
	ff = first.id;
	lagged = lag1(code);
	if first.id then code_cnt = 1;
	else do;
		if code ~= lag1(code) then code_cnt = code_cnt + 1;
		end;
PROC PRINT;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The resulting dataset looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Obs id code code_cnt ff lagged&lt;/P&gt;&lt;TABLE cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;abc&lt;/TD&gt;&lt;TD&gt;1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Y&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What is wrong?&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 16:45:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-comparison-problems-with-lag-functions/m-p/246133#M46010</guid>
      <dc:creator>CJ_Jackson</dc:creator>
      <dc:date>2016-01-26T16:45:06Z</dc:date>
    </item>
    <item>
      <title>Re: String comparison problems with lag functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-comparison-problems-with-lag-functions/m-p/246137#M46014</link>
      <description>The count (distinct code) construct in SQL seems better suited for this kind of calculation.</description>
      <pubDate>Tue, 26 Jan 2016 16:52:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-comparison-problems-with-lag-functions/m-p/246137#M46014</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-01-26T16:52:10Z</dc:date>
    </item>
    <item>
      <title>Re: String comparison problems with lag functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-comparison-problems-with-lag-functions/m-p/246141#M46017</link>
      <description>&lt;P&gt;I agree that proc sql is generally a better solution for most types of aggregation. But, if it must be in a data step for some reason, then it would look something like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA test;
	input id code $;
	cards;
	1 abc
	1 abc
	2 xyz
	2 xyy
	2 xyy
	2 zzz
	3 ccc
	3 ccc
	;
PROC SORT;
	by id code;
run;

DATA test2;
	SET test;
	by id code;
	retain code_cnt;
	if first.id then code_cnt = 0;
	code_cnt = code_cnt + 1;
	if last.id then output;
run;	

PROC PRINT;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is probably what the proc sql will be doing in the background as well.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 17:01:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-comparison-problems-with-lag-functions/m-p/246141#M46017</guid>
      <dc:creator>DanZ</dc:creator>
      <dc:date>2016-01-26T17:01:19Z</dc:date>
    </item>
    <item>
      <title>Re: String comparison problems with lag functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-comparison-problems-with-lag-functions/m-p/246142#M46018</link>
      <description>&lt;P&gt;You can't use lag in IF/THEN conditions, the behaviour isn't what you expect. It's a common issue when first starting out with the lag function. If you google the terms you'll find many paper/posts on the issue.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA test2;
	SET test;
	by id code;
	RETAIN code_cnt 1;
	ff = first.id;
	lagged = lag1(code);
	if first.id then code_cnt = 1;
	else do;
		if code ne lagged then code_cnt = code_cnt + 1;
		end;
PROC PRINT;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As mentioned PROC SQL is a better method here:&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 id, count(distinct code) as code_cnt
from test;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or a SAS proc method:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=test out=test2 nodupkey;
by id code;
run;

proc freq data=test2;
table ID/out=want_freq;
run;

proc print data=want_freq;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT: One more option - based on using FIRST since you've sorted and are using BY variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SORT;
	by id code;
run;

data want;
set test;
by id code;
if first.id then code_cnt=0;
if first.code then code_cnt+1;
*if last.id then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 17:05:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-comparison-problems-with-lag-functions/m-p/246142#M46018</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-26T17:05:33Z</dc:date>
    </item>
    <item>
      <title>Re: String comparison problems with lag functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-comparison-problems-with-lag-functions/m-p/246161#M46034</link>
      <description>&lt;P&gt;The lag() function builds a FIFO chain that receives a value &lt;U&gt;everytime the function is called&lt;/U&gt;. So calling the function in a conditional branch or a do loop will lead to unexpected behaviour.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 17:56:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-comparison-problems-with-lag-functions/m-p/246161#M46034</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-01-26T17:56:18Z</dc:date>
    </item>
    <item>
      <title>Re: String comparison problems with lag functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-comparison-problems-with-lag-functions/m-p/246165#M46035</link>
      <description>thanks. This is a weird behavior. So if I first saved the lag1(code) as a new variable then I would be able to use that value in a conditional then, correct?</description>
      <pubDate>Tue, 26 Jan 2016 18:13:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-comparison-problems-with-lag-functions/m-p/246165#M46035</guid>
      <dc:creator>CJ_Jackson</dc:creator>
      <dc:date>2016-01-26T18:13:23Z</dc:date>
    </item>
    <item>
      <title>Re: String comparison problems with lag functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-comparison-problems-with-lag-functions/m-p/246177#M46040</link>
      <description>&lt;P&gt;Thanks, your solutions (mostly) worked. In the PROC SQL you wanna do GROUP BY id as well.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 18:35:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-comparison-problems-with-lag-functions/m-p/246177#M46040</guid>
      <dc:creator>CJ_Jackson</dc:creator>
      <dc:date>2016-01-26T18:35:08Z</dc:date>
    </item>
    <item>
      <title>Re: String comparison problems with lag functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-comparison-problems-with-lag-functions/m-p/246252#M46050</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/69133"&gt;@CJ_Jackson&lt;/a&gt; wrote:&lt;BR /&gt;thanks. This is a weird behavior. So if I first saved the lag1(code) as a new variable then I would be able to use that value in a conditional then, correct?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, exactly as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; did with the variable LAGGED in the first data step of the solution.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2016 00:24:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-comparison-problems-with-lag-functions/m-p/246252#M46050</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-01-27T00:24:28Z</dc:date>
    </item>
  </channel>
</rss>

