<?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 Calculate number of changes in score during 6 months in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-changes-in-score-during-6-months/m-p/540352#M149042</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;For each customer there are 6 rows for scores in 6 months.&lt;/P&gt;
&lt;P&gt;I want to calculate for each customer number of changes in score during 6 months.&lt;/P&gt;
&lt;P&gt;for example:&lt;/P&gt;
&lt;P&gt;ID 1 -there were 2 changes during the period (from 9 to 8 and from &amp;nbsp;8 to 9)&lt;/P&gt;
&lt;P&gt;ID 2 -there were 3 changes during the period (from 7 to 6 and from &amp;nbsp;6 to 7 and from 7 to &lt;span class="lia-unicode-emoji" title=":smiling_face_with_sunglasses:"&gt;😎&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;ID 3 -there were 2 changes during the period (from . to 9 and from &amp;nbsp;9 to 10&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data rawdata;
Input ID  month score;
cards;
1 1807 9
1 1808 9
1 1809 9
1 1810 8
1 1811 9
1 1812 9
2 1807 7
2 1808 6
2 1809 7
2 1810 7
2 1811 8
2 1812 8
3 1807 .
3 1808 9 
3 1809 9
3 1810 9
3 1811 10
3 1812 10
;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;)&lt;/P&gt;</description>
    <pubDate>Tue, 05 Mar 2019 07:49:41 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2019-03-05T07:49:41Z</dc:date>
    <item>
      <title>Calculate number of changes in score during 6 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-changes-in-score-during-6-months/m-p/540352#M149042</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;For each customer there are 6 rows for scores in 6 months.&lt;/P&gt;
&lt;P&gt;I want to calculate for each customer number of changes in score during 6 months.&lt;/P&gt;
&lt;P&gt;for example:&lt;/P&gt;
&lt;P&gt;ID 1 -there were 2 changes during the period (from 9 to 8 and from &amp;nbsp;8 to 9)&lt;/P&gt;
&lt;P&gt;ID 2 -there were 3 changes during the period (from 7 to 6 and from &amp;nbsp;6 to 7 and from 7 to &lt;span class="lia-unicode-emoji" title=":smiling_face_with_sunglasses:"&gt;😎&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;ID 3 -there were 2 changes during the period (from . to 9 and from &amp;nbsp;9 to 10&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data rawdata;
Input ID  month score;
cards;
1 1807 9
1 1808 9
1 1809 9
1 1810 8
1 1811 9
1 1812 9
2 1807 7
2 1808 6
2 1809 7
2 1810 7
2 1811 8
2 1812 8
3 1807 .
3 1808 9 
3 1809 9
3 1810 9
3 1811 10
3 1812 10
;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;)&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2019 07:49:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-changes-in-score-during-6-months/m-p/540352#M149042</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-03-05T07:49:41Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate number of changes in score during 6 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-changes-in-score-during-6-months/m-p/540357#M149044</link>
      <description>&lt;P&gt;Use by-group processing, the lag() function, and an automatically retained variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data rawdata;
Input ID  month score;
cards;
1 1807 9
1 1808 9
1 1809 9
1 1810 8
1 1811 9
1 1812 9
2 1807 7
2 1808 6
2 1809 7
2 1810 7
2 1811 8
2 1812 8
3 1807 .
3 1808 9 
3 1809 9
3 1810 9
3 1811 10
3 1812 10
;
run;

data want;
set rawdata;
by id;
oldscore = lag(score);
if first.id then changes = 0;
else if score ne oldscore then changes + 1;
if last.id then output;
keep id changes;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;ID    changes

 1       2   
 2       3   
 3       2   
&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Mar 2019 08:21:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-changes-in-score-during-6-months/m-p/540357#M149044</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-05T08:21:26Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate number of changes in score during 6 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-changes-in-score-during-6-months/m-p/540363#M149045</link>
      <description>&lt;P&gt;Are there always exactly 6 months of data or would there be some sort of rolling window?&amp;nbsp; I would suggest creating a change from previous result flag, then you can simply sum() the flags based on a 6 month window, perhaps something like:&lt;/P&gt;
&lt;PRE&gt;data have;
  set rawdata;
  by id;
  if not first.id and score ne lag(score) then chg_flg=1;
run;&lt;/PRE&gt;
&lt;P&gt;In this way you can say sum chg_flg over start of period to end of period = changes in the 6 months.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2019 08:28:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-changes-in-score-during-6-months/m-p/540363#M149045</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-03-05T08:28:57Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate number of changes in score during 6 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-changes-in-score-during-6-months/m-p/540408#M149073</link>
      <description>&lt;P&gt;Thank you both for very nice solutions that you sent.&lt;/P&gt;
&lt;P&gt;I just summarize your solutions here.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data rawdata;
Input ID  month score;
cards;
1 1807 9
1 1808 9
1 1809 9
1 1810 8
1 1811 9
1 1812 9
2 1807 7
2 1808 6
2 1809 7
2 1810 7
2 1811 8
2 1812 8
3 1807 .
3 1808 9 
3 1809 9
3 1810 9
3 1811 10
3 1812 10
;
run;

/****Way1******/
/****Way1******/
/****Way1******/
/****Way1******/
Data want;
set rawdata;
by id;
oldscore = lag(score);
if first.id then No_changes = 0;
else if score ne oldscore then No_changes + 1;
if last.id then output;
keep id No_changes;
run;
proc print data=want noobs;
run;


/****Way2******/
/****Way2******/
/****Way2******/
/****Way2******/
data HelpTbl;
  set rawdata;
  by id;
  if not first.id and score ne lag(score) then chg_flg=1;
run;
PROC SQL;
	create table want2  as
	select  ID,
          sum( chg_flg) as No_changes
	from  HelpTbl
	group by ID
;
QUIT;
proc print data=want2 noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Mar 2019 11:19:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-changes-in-score-during-6-months/m-p/540408#M149073</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-03-05T11:19:38Z</dc:date>
    </item>
  </channel>
</rss>

