<?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: Count new customers,were and stay,were left and back in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-new-customers-were-and-stay-were-left-and-back/m-p/707448#M217211</link>
    <description>Sorry did not see the rest of the question at bottom of post.  My example only does the calculation based on 2018.</description>
    <pubDate>Mon, 21 Dec 2020 13:41:26 GMT</pubDate>
    <dc:creator>CarmineVerrell</dc:creator>
    <dc:date>2020-12-21T13:41:26Z</dc:date>
    <item>
      <title>Count new customers,were and stay,were left and back</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-new-customers-were-and-stay-were-left-and-back/m-p/707433#M217201</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I have raw data sets in each month with list of customer Id.&lt;/P&gt;
&lt;P&gt;I want to classify each customer in each data set to one of 3 groups:&lt;/P&gt;
&lt;P&gt;1-How many customers in this file that have never been before in previous months&lt;/P&gt;
&lt;P&gt;(Call it "New customers never been before")&lt;/P&gt;
&lt;P&gt;2-How many customers in this file that have been in previous months but were not in last month(Call it "Were ,Left and Back customers")&lt;/P&gt;
&lt;P&gt;3-How many customers in this file that have been in last month&lt;/P&gt;
&lt;P&gt;(Call it "Were and Stay customers")&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then for each month I want to calculate 4 statistics:&lt;/P&gt;
&lt;P&gt;1-How many customers in the file&lt;/P&gt;
&lt;P&gt;2-How many customers belong to category "New customers never been before"&lt;/P&gt;
&lt;P&gt;3-How many customers belong to category "Were Left and Back customers"&lt;/P&gt;
&lt;P&gt;3-How many customers belong to category "Were and Stay customers"&lt;/P&gt;
&lt;P&gt;4-How many customers left this month and never came back&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data t2004;
input ID;
cards;
1
2
;
Run;
Data t2005;
input ID;
cards;
1
2
3
;
Run;
Data t2006;
input ID;
cards;
1
2
4
;
Run;
Data t2007;
input ID;
cards;
2
4
5
6
7
;
Run;
Data t2008;
input ID;
cards;
2
4
6
1
;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example :&lt;/P&gt;
&lt;P&gt;for 2004:&lt;/P&gt;
&lt;P&gt;1-How many customers in the file:2&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2-How many customers belong to category "New customers never been before":0&lt;/P&gt;
&lt;P&gt;3-How many customers belong to category "Were, Left and Back customers":0&lt;/P&gt;
&lt;P&gt;3-How many customers belong to category "Were and Stay customers":0&lt;/P&gt;
&lt;P&gt;&amp;nbsp;for 2005:&lt;BR /&gt;1-How many customers in the file:3&amp;nbsp;&lt;BR /&gt;2-How many customers belong to category "New customers never been before":1&lt;BR /&gt;3-How many customers belong to category "Were, Left and Back customers":0&lt;BR /&gt;3-How many customers belong to category "Were and Stay customers":2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;for 2006:&lt;BR /&gt;1-How many customers in the file:3&amp;nbsp;&lt;BR /&gt;2-How many customers belong to category "New customers never been before":1&lt;BR /&gt;3-How many customers belong to category "Were, Left and Back customers":0&lt;BR /&gt;3-How many customers belong to category "Were and Stay customers":2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;for 2007:&lt;BR /&gt;1-How many customers in the file:5&amp;nbsp;&lt;BR /&gt;2-How many customers belong to category "New customers never been before": 3&lt;BR /&gt;3-How many customers belong to category "Were, Left and Back customers": 0&lt;BR /&gt;3-How many customers belong to category "Were and Stay customers":2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;for 2008:&lt;BR /&gt;1-How many customers in the file:4&amp;nbsp;&lt;BR /&gt;2-How many customers belong to category "New customers never been before":&amp;nbsp;&lt;BR /&gt;3-How many customers belong to category "Were, Left and Back customers": 1&amp;nbsp;&lt;BR /&gt;3-How many customers belong to category "Were and Stay customers":3&lt;/P&gt;</description>
      <pubDate>Mon, 21 Dec 2020 13:16:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-new-customers-were-and-stay-were-left-and-back/m-p/707433#M217201</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-12-21T13:16:25Z</dc:date>
    </item>
    <item>
      <title>Re: Count new customers,were and stay,were left and back</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-new-customers-were-and-stay-were-left-and-back/m-p/707443#M217206</link>
      <description>&lt;P&gt;I know to identify customer who "were and stay"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
Data tbl1;
set t2004 t2005 t2006 t2007 t2008
indsname=ds
;
source=substr(scan(ds,2,'.'),2,4);
Run;
 
PROC SQL;
	create table Help1  as
	select  ID,
            count(*) as nr_appear
	from  tbl1
	group by ID 
;
QUIT;

PROC SQL;
	create table tbl  as
	select  a.*,b.nr_appear,case when b.nr_appear=5 then 'Was and stay' end as Ind	   
	from tbl1 as a
    left join  Help1 as b
	on a.ID=b.ID
	order by a.source
;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Dec 2020 13:29:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-new-customers-were-and-stay-were-left-and-back/m-p/707443#M217206</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-12-21T13:29:54Z</dc:date>
    </item>
    <item>
      <title>Re: Count new customers,were and stay,were left and back</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-new-customers-were-and-stay-were-left-and-back/m-p/707447#M217210</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Modified your code below to include ID 6 in T2004 as there are no examples &lt;BR /&gt;of "Were left and Back Customers".*/&lt;BR /&gt;/**/&lt;BR /&gt;/*Please see below, for possible solution code. Hope this helps.*/&lt;BR /&gt;/**/&lt;BR /&gt;/*Carmine*/&lt;BR /&gt;/*If this is an acceptable solution, please let us know.*/&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Data t2004;
input ID;
cards;
1
2
6
;
Run;
Data t2005;
input ID;
cards;
1
2
3
;
Run;
Data t2006;
input ID;
cards;
1
2
4
;
Run;
Data t2007;
input ID;
cards;
2
4
5
7
;
Run;
Data t2008;
input ID;
cards;
2
4
6
8
;
Run;


data newc(drop=stattxt) leftb(drop=stattxt) Stayc(drop=stattxt) stats;
	merge t2004(in=in2004)
	      t2005(in=in2005)
		  t2006(in=in2006)
	      t2007(in=in2007)
	      t2008(in=in2008);
	by id;
	length stattxt $40;
		if in2008 and sum(of in2004-in2007)=0 then do; stattxt="New customers never been before";output stats; output newc;end;
	    if in2008 and not in2007 and sum(of in2004-in2006)&amp;gt;0 then do;stattxt="Were Left and Back Customers";output stats; output leftb; end;
		if in2008 and in2007 then do; stattxt="Were and Stay Customers";output stats; output stayc; end;
run;

Proc Freq data=stats;
table stattxt;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Dec 2020 13:37:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-new-customers-were-and-stay-were-left-and-back/m-p/707447#M217210</guid>
      <dc:creator>CarmineVerrell</dc:creator>
      <dc:date>2020-12-21T13:37:58Z</dc:date>
    </item>
    <item>
      <title>Re: Count new customers,were and stay,were left and back</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-new-customers-were-and-stay-were-left-and-back/m-p/707448#M217211</link>
      <description>Sorry did not see the rest of the question at bottom of post.  My example only does the calculation based on 2018.</description>
      <pubDate>Mon, 21 Dec 2020 13:41:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-new-customers-were-and-stay-were-left-and-back/m-p/707448#M217211</guid>
      <dc:creator>CarmineVerrell</dc:creator>
      <dc:date>2020-12-21T13:41:26Z</dc:date>
    </item>
    <item>
      <title>Re: Count new customers,were and stay,were left and back</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-new-customers-were-and-stay-were-left-and-back/m-p/707450#M217212</link>
      <description>Meant to say 2008.</description>
      <pubDate>Mon, 21 Dec 2020 13:42:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-new-customers-were-and-stay-were-left-and-back/m-p/707450#M217212</guid>
      <dc:creator>CarmineVerrell</dc:creator>
      <dc:date>2020-12-21T13:42:31Z</dc:date>
    </item>
    <item>
      <title>Re: Count new customers,were and stay,were left and back</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-new-customers-were-and-stay-were-left-and-back/m-p/707455#M217215</link>
      <description>&lt;P&gt;What is the way to do it for each month?&lt;/P&gt;
&lt;P&gt;Can you please explain why in each block (IF do end) you write 2 output statements?&lt;/P&gt;
&lt;P&gt;for example:&lt;/P&gt;
&lt;P&gt;output stats;&lt;BR /&gt;output newc;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Dec 2020 14:01:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-new-customers-were-and-stay-were-left-and-back/m-p/707455#M217215</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-12-21T14:01:20Z</dc:date>
    </item>
    <item>
      <title>Re: Count new customers,were and stay,were left and back</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-new-customers-were-and-stay-were-left-and-back/m-p/707469#M217220</link>
      <description>&lt;P&gt;This is not the best solution, but i don't have the time to think about it in detail.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.all_years / view=work.all_years;
   length 
      _source $ 42 
      year 8
   ;
   
   set work.t: indsname=_source;
   
   year = input(compress(_source,, 'kd'), 4.);
run;

proc sort data=work.all_years out=work.sorted;
   by id year;
run;


data work.pre_want;
   set work.sorted;
   by id;
   
   length flag2-flag4 8; 
   
   flag2 = first.id;
   flag3 = (not first.id and year-1 &amp;gt; lag(year));
   flag4 = (not first.id and year-1 = lag(year));
run;


proc report data=work.pre_want;
   column year year=flag1 flag2 flag3 flag4;
   define year / group;
   define flag1 / n 'How many customers in the file';
   define flag2 / sum 'New customers never been before';
   define flag3 / sum "Were, Left and Back customers";
   define flag4 / sum "Were and Stay customers";
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Dec 2020 14:16:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-new-customers-were-and-stay-were-left-and-back/m-p/707469#M217220</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-12-21T14:16:53Z</dc:date>
    </item>
    <item>
      <title>Re: Count new customers,were and stay,were left and back</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-new-customers-were-and-stay-were-left-and-back/m-p/707496#M217226</link>
      <description>&lt;P&gt;Can I ask, If you use else IF instead of "IF"&amp;nbsp; will it be also okay?&lt;/P&gt;
&lt;P&gt;Why are you usinf multiple IF statements and not If&amp;nbsp; &amp;nbsp;and then else if..?&lt;/P&gt;</description>
      <pubDate>Mon, 21 Dec 2020 15:06:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-new-customers-were-and-stay-were-left-and-back/m-p/707496#M217226</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-12-21T15:06:49Z</dc:date>
    </item>
    <item>
      <title>Re: Count new customers,were and stay,were left and back</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-new-customers-were-and-stay-were-left-and-back/m-p/707580#M217265</link>
      <description>Why are you using "BY ID"?&lt;BR /&gt;As I see we can SET the data sets without BY?&lt;BR /&gt;In this example is it essential to use BY?</description>
      <pubDate>Mon, 21 Dec 2020 21:50:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-new-customers-were-and-stay-were-left-and-back/m-p/707580#M217265</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-12-21T21:50:18Z</dc:date>
    </item>
  </channel>
</rss>

