<?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 for each customer Number of times that he touch grade 11 or grade 12 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculate-for-each-customer-Number-of-times-that-he-touch-grade/m-p/540411#M149076</link>
    <description>&lt;P&gt;Hello&lt;BR /&gt;I have multiple observations for each customer .(6 rows for each customer in months :1807,1808,1809,1810,1811,1812)&lt;BR /&gt;I want to calculate for each customer Number of times that he touch grade 11 or grade 12.&lt;BR /&gt;For ID 1 I expect to get &amp;nbsp;value 3&lt;BR /&gt;For ID 2 I expect to get &amp;nbsp;value 0&lt;BR /&gt;For ID 3 I expect to get &amp;nbsp;value 1&lt;BR /&gt;May anyone show please How to do it in one step?&lt;BR /&gt;In the following code I show how to do it in two steps.&lt;BR /&gt;My challenge is to learn to do it in one step with lag or another way,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data rawdata;
Input ID  month score;
cards;
1 1807 11
1 1808 12
1 1809 9
1 1810 8
1 1811 11
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 11
;
run;

/*Way1*/
Data tbl2;
set rawdata;
IF score in(11,12) then Touch_11_12_flag=1;
else Touch_11_12_flag=0;
Run;
PROC SQL;
	create table want1  as
	select ID, 
             sum(Touch_11_12_flag) as Total_Touch_11_12
	from  tbl2
	group by ID
;
QUIT;
proc print data=want1 noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 05 Mar 2019 11:36:13 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2019-03-05T11:36:13Z</dc:date>
    <item>
      <title>calculate for each customer Number of times that he touch grade 11 or grade 12</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-for-each-customer-Number-of-times-that-he-touch-grade/m-p/540411#M149076</link>
      <description>&lt;P&gt;Hello&lt;BR /&gt;I have multiple observations for each customer .(6 rows for each customer in months :1807,1808,1809,1810,1811,1812)&lt;BR /&gt;I want to calculate for each customer Number of times that he touch grade 11 or grade 12.&lt;BR /&gt;For ID 1 I expect to get &amp;nbsp;value 3&lt;BR /&gt;For ID 2 I expect to get &amp;nbsp;value 0&lt;BR /&gt;For ID 3 I expect to get &amp;nbsp;value 1&lt;BR /&gt;May anyone show please How to do it in one step?&lt;BR /&gt;In the following code I show how to do it in two steps.&lt;BR /&gt;My challenge is to learn to do it in one step with lag or another way,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data rawdata;
Input ID  month score;
cards;
1 1807 11
1 1808 12
1 1809 9
1 1810 8
1 1811 11
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 11
;
run;

/*Way1*/
Data tbl2;
set rawdata;
IF score in(11,12) then Touch_11_12_flag=1;
else Touch_11_12_flag=0;
Run;
PROC SQL;
	create table want1  as
	select ID, 
             sum(Touch_11_12_flag) as Total_Touch_11_12
	from  tbl2
	group by ID
;
QUIT;
proc print data=want1 noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2019 11:36:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-for-each-customer-Number-of-times-that-he-touch-grade/m-p/540411#M149076</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-03-05T11:36:13Z</dc:date>
    </item>
    <item>
      <title>Re: calculate for each customer Number of times that he touch grade 11 or grade 12</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-for-each-customer-Number-of-times-that-he-touch-grade/m-p/540412#M149077</link>
      <description>&lt;P&gt;It's the same approach with a retained variable that worked for you in &lt;A href="https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-changes-in-score-during-6-months/m-p/540352" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-changes-in-score-during-6-months/m-p/540352&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2019 11:42:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-for-each-customer-Number-of-times-that-he-touch-grade/m-p/540412#M149077</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-05T11:42:19Z</dc:date>
    </item>
    <item>
      <title>Re: calculate for each customer Number of times that he touch grade 11 or grade 12</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-for-each-customer-Number-of-times-that-he-touch-grade/m-p/540447#M149095</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data rawdata;
Input ID  month score;
cards;
1 1807 11
1 1808 12
1 1809 9
1 1810 8
1 1811 11
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 11
;
run;

PROC SQL;
	create table want1  as
	select ID, 
             sum(score in(11,12)) as Total_Touch_11_12
	from  rawdata
	group by ID
;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Mar 2019 14:11:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-for-each-customer-Number-of-times-that-he-touch-grade/m-p/540447#M149095</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-03-05T14:11:46Z</dc:date>
    </item>
    <item>
      <title>Re: calculate for each customer Number of times that he touch grade 11 or grade 12</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-for-each-customer-Number-of-times-that-he-touch-grade/m-p/540620#M149167</link>
      <description>&lt;P&gt;I see that &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; noticed the same problem from you already in sas communities.&amp;nbsp; Is there a reason to post it again?&amp;nbsp; The usual result of this approach is to see subsequent new questions ignored - like the boy who cried wolf.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Help us help you - don't overload the people with useful answers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards,&lt;/P&gt;
&lt;P&gt;MK&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2019 23:59:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-for-each-customer-Number-of-times-that-he-touch-grade/m-p/540620#M149167</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-03-05T23:59:36Z</dc:date>
    </item>
    <item>
      <title>Re: calculate for each customer Number of times that he touch grade 11 or grade 12</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-for-each-customer-Number-of-times-that-he-touch-grade/m-p/540655#M149180</link>
      <description>&lt;P&gt;Thank you so much!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want3;
set rawdata;
by id;
oldscore = lag(score);
if first.id and score in(11,12) then Total_Touch_11_12 = 1;
else if first.id and score not in(11,12) then Total_Touch_11_12 = 0;
else if score in(11,12) then Total_Touch_11_12 + 1;
if last.id then output;
keep id Total_Touch_11_12;
run;
proc print data=want3 noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Mar 2019 06:15:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-for-each-customer-Number-of-times-that-he-touch-grade/m-p/540655#M149180</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-03-06T06:15:09Z</dc:date>
    </item>
    <item>
      <title>Re: calculate for each customer Number of times that he touch grade 11 or grade 12</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-for-each-customer-Number-of-times-that-he-touch-grade/m-p/540666#M149187</link>
      <description>&lt;P&gt;You're overcomplicating. Such a step has three discrete elements:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;initialize counter at first.id&lt;/LI&gt;
&lt;LI&gt;increment counter on condition&lt;/LI&gt;
&lt;LI&gt;output at last.id&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;So the step can easily be written as&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want3;
set rawdata;
by id;
if first.id then total_touch_11_12 = 0;
if score in (11,12) then total_touch_11_12 + 1;
if last.id then output;
keep id total_touch_11_12;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that the increment statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;total_touch_11_12 + 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;implies an automatic retain. If you need to do another type of calculation, you will need an explicit retain statement.&lt;/P&gt;
&lt;P&gt;Since oldscore is not needed here (there's no comparison with a previous observation (to detect a change) in your logic), the assignment with the lag() function can also be omitted.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2019 07:07:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-for-each-customer-Number-of-times-that-he-touch-grade/m-p/540666#M149187</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-06T07:07:07Z</dc:date>
    </item>
    <item>
      <title>Re: calculate for each customer Number of times that he touch grade 11 or grade 12</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-for-each-customer-Number-of-times-that-he-touch-grade/m-p/540700#M149203</link>
      <description>Kurt&lt;BR /&gt;Why not introduce the OP to what has neen referred to as the DoW loop, eliminating the need for initialising and first. Code&lt;BR /&gt;The following assumes only that data is in ID order and any score above 10 is enough.&lt;BR /&gt;Data want ;&lt;BR /&gt; Do until( last.ID )&lt;BR /&gt;   set rawdata ;&lt;BR /&gt;   BY id ;&lt;BR /&gt;   if score &amp;gt; 10 then touch11 = sum( 1, touch11 ) ;&lt;BR /&gt;  end ;&lt;BR /&gt;  If touch11 ;&lt;BR /&gt;Run;&lt;BR /&gt;</description>
      <pubDate>Wed, 06 Mar 2019 08:53:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-for-each-customer-Number-of-times-that-he-touch-grade/m-p/540700#M149203</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2019-03-06T08:53:04Z</dc:date>
    </item>
    <item>
      <title>Re: calculate for each customer Number of times that he touch grade 11 or grade 12</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-for-each-customer-Number-of-times-that-he-touch-grade/m-p/540707#M149209</link>
      <description>&lt;P&gt;I'd write that as&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until (last.id);
  set rawdata;
  by id;
  if score in (11,12) then touch11 = sum(touch11,1);
end;
touch11 = max(0,touch11); * to prevent missing values;
keep id touch11;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Both the "classical" data step method (first,increment,last) and the DOW loop require sorting and need only a single sequential pass through the sorted dataset, and are therefore functionally equivalent with (probably) only marginal differences in performance.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2019 09:08:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-for-each-customer-Number-of-times-that-he-touch-grade/m-p/540707#M149209</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-06T09:08:12Z</dc:date>
    </item>
    <item>
      <title>Re: calculate for each customer Number of times that he touch grade 11 or grade 12</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-for-each-customer-Number-of-times-that-he-touch-grade/m-p/540717#M149213</link>
      <description>Like brevity&lt;BR /&gt;&lt;BR /&gt;Data want ;&lt;BR /&gt;Touch11=0;&lt;BR /&gt;Do until( last.ID )&lt;BR /&gt;set rawdata ;&lt;BR /&gt;BY id ;&lt;BR /&gt;if score &amp;gt; 10 then touch11 +1 ;&lt;BR /&gt;end ;&lt;BR /&gt;Run;&lt;BR /&gt;&lt;BR /&gt;Interestingly the sample data appear to be in month order within ID. However, I cannot see a way to take advantage of the month-order.&lt;BR /&gt;</description>
      <pubDate>Wed, 06 Mar 2019 09:57:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-for-each-customer-Number-of-times-that-he-touch-grade/m-p/540717#M149213</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2019-03-06T09:57:08Z</dc:date>
    </item>
  </channel>
</rss>

