<?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: Find for each customer missing months in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-for-each-customer-missing-months/m-p/879514#M347456</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input CustID YYMM;
cards;
111 2201
111 2202
111 2203
111 2204
222 2203
222 2204
222 2206
222 2207
222 2208
222 2209
222 2210
333 2206
333 2207
444 2205
444 2207
444 2208
444 2209
444 2210
444 2212
;
Run;
data temp;
 set have;
date=mdy(mod(YYMM,100),1,int(YYMM/100));
format date yymmn4.;
drop YYMM;
run;
proc summary data=temp ;
by CustID;
var date;
output out=temp2(drop=_:) min=min max=max;
run;
data temp3;
 set temp2;
do date=min to max;
 if month ne month(date) then output;
 month=month(date);
end;
format date yymmn4.;
drop min max month;
run;
proc sql;
create table want as
select distinct CustID,put(date,yymmn4. -l) as yymm  from (select * from temp3 except select * from temp)
union
select distinct CustID,'No' from temp where CustID not in (select CustID from (select * from temp3 except select * from temp))
;
quit;
data final_want;
 do until(last.CustID);
  set want;
  by CustID;
 length missing_Months_Vector $ 200;
 missing_Months_Vector=catx(',',missing_Months_Vector,yymm);
 end;
 drop yymm;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 07 Jun 2023 11:33:55 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2023-06-07T11:33:55Z</dc:date>
    <item>
      <title>Find for each customer missing months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-for-each-customer-missing-months/m-p/879488#M347443</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;For each customer ID there are multiple rows (Each row represent information in different time).&lt;/P&gt;
&lt;P&gt;YYMM&amp;nbsp; column is year+month in structure YYMM (numeric value).&lt;/P&gt;
&lt;P&gt;I want to find for each customer which months (YYMM) are missing between the first month and last month.&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;For customer 111&lt;/P&gt;
&lt;P&gt;first month is 2201&lt;/P&gt;
&lt;P&gt;Last month is 2204&lt;/P&gt;
&lt;P&gt;And all months between 2201 and 2204 appear so the new column will get value "No"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Customer 222&lt;/P&gt;
&lt;P&gt;First month 2203&lt;/P&gt;
&lt;P&gt;last month 2210&lt;/P&gt;
&lt;P&gt;Month 2205 is missing so new column should get value 2205&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Customer 444&lt;BR /&gt;First month 2205&lt;BR /&gt;last month 2212&lt;BR /&gt;Months 2206 and 2211&amp;nbsp; are&amp;nbsp; missing so new column should get value 2206-2211&lt;/P&gt;
&lt;P&gt;What is the way to create the wanted data set?&lt;/P&gt;
&lt;P&gt;In the wanted data set each customer will have one row&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 have;
input CustID YYMM;
cards;
111 2201
111 2202
111 2203
111 2204
222 2203
222 2204
222 2206
222 2207
222 2208
222 2209
222 2210
333 2206
333 2207
444 2205
444 2207
444 2208
444 2209
444 2210
444 2212
;
Run;


data want;
infile datalines dlm="," dsd;
input CustID missing_Months_Vector $;
cards;
111,No 
222,2205
333,No
444,2206-2211
;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2023 07:36:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-for-each-customer-missing-months/m-p/879488#M347443</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-06-07T07:36:17Z</dc:date>
    </item>
    <item>
      <title>Re: Find for each customer missing months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-for-each-customer-missing-months/m-p/879491#M347445</link>
      <description>&lt;P&gt;Don't create any form of "wide" dataset; Long Beats Wide (Maxim 19).&lt;/P&gt;
&lt;P&gt;And NEVER (&lt;STRONG&gt;NEVER!!!&lt;/STRONG&gt;) store date values in stupid numbers like these; use SAS date values!&lt;/P&gt;
&lt;P&gt;With SAS date values, this should do it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
merge
  have
  have (
    firstobs=2
    rename=(custid=_cust yymm=_yymm)
  )
;
if _cust = custid
then do;
  yymm = intnx('month`,yymm,1,'b');
  do while (yymm lt _yymm);
    output;
    yymm = intnx('month',yymm,1,'b');
  end;
end;
keep custid yymm;
run;
  &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Jun 2023 08:25:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-for-each-customer-missing-months/m-p/879491#M347445</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-06-07T08:25:35Z</dc:date>
    </item>
    <item>
      <title>Re: Find for each customer missing months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-for-each-customer-missing-months/m-p/879498#M347448</link>
      <description>&lt;P&gt;I&amp;nbsp; added another field with sas date instead of numeric YYMM.&lt;/P&gt;
&lt;P&gt;I run your code but I cannot see any result (get empty data set).&lt;/P&gt;
&lt;P&gt;The reason that wanted data set is wide is that I want to print the wanted data set and see for each customer one row only&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input CustID YYMM   yymm2 : date9.;
format yymm2    ddmmyy10.;
cards;
111 2201 '01JAN2022'd
111 2202 '01FEB2022'd
111 2203 '01MAR2022'd
111 2204 '01APR2022'd
222 2203 '01MAR2022'd
222 2204 '01APR2022'd
222 2206 '01JUN2022'd
222 2207 '01JUL2022'd
222 2208 '01AUG2022'd
222 2209 '01SEP2022'd
222 2210 '01OCT2022'd
;
Run;

data want;
merge have have (firstobs=2rename=(custid=_cust yymm=_yymm));
if _cust = custid then do;
yymm = intnx('month',yymm,1,'b');
do while (yymm&amp;lt;=_yymm);
    output;
    yymm = intnx('month',yymm,1,'b');
  end;
end;
keep custid yymm;
run;
  
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Jun 2023 09:02:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-for-each-customer-missing-months/m-p/879498#M347448</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-06-07T09:02:01Z</dc:date>
    </item>
    <item>
      <title>Re: Find for each customer missing months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-for-each-customer-missing-months/m-p/879503#M347450</link>
      <description>&lt;P&gt;When you create an extra date variable, you should then also use it in the code in place of the original yymm.&lt;/P&gt;
&lt;P&gt;But you do not need two separate variables. There are SAS formats to display dates as year/month only.&lt;/P&gt;
&lt;P&gt;And, as you said, you want to later&amp;nbsp;&lt;EM&gt;print&lt;/EM&gt; the data in wide format, do this with a reporting procedure.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2023 10:01:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-for-each-customer-missing-months/m-p/879503#M347450</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-06-07T10:01:24Z</dc:date>
    </item>
    <item>
      <title>Re: Find for each customer missing months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-for-each-customer-missing-months/m-p/879507#M347451</link>
      <description>&lt;P&gt;Sorry for that but may you please show the code from start till end because I run the code and it is not working for me&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2023 10:27:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-for-each-customer-missing-months/m-p/879507#M347451</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-06-07T10:27:50Z</dc:date>
    </item>
    <item>
      <title>Re: Find for each customer missing months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-for-each-customer-missing-months/m-p/879509#M347452</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;For each customer ID there are multiple rows (Each row represent information in different time).&lt;/P&gt;
&lt;P&gt;YYMM&amp;nbsp; column is year+month in structure YYMM (numeric value).&lt;/P&gt;
&lt;P&gt;I want to find for each customer which months (YYMM) are missing between the first month and last month.&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;For customer 111&lt;/P&gt;
&lt;P&gt;first month is 2201&lt;/P&gt;
&lt;P&gt;Last month is 2204&lt;/P&gt;
&lt;P&gt;And all months between 2201 and 2204 appear so the new column will get value "No"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Customer 222&lt;/P&gt;
&lt;P&gt;First month 2203&lt;/P&gt;
&lt;P&gt;last month 2210&lt;/P&gt;
&lt;P&gt;Month 2205 is missing so new column should get value 2205&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Customer 444&lt;BR /&gt;First month 2205&lt;BR /&gt;last month 2212&lt;BR /&gt;Months 2206 and 2211&amp;nbsp; are&amp;nbsp; missing so new column should get value 2206-2211&lt;/P&gt;
&lt;P&gt;What is the way to create the wanted data set?&lt;/P&gt;
&lt;P&gt;In the wanted data set each customer will have one row&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It seems like you want a new column to say "No" when there are no missing months; it should say "2205" (a character string) for 222 and for 444 you want the character string "2206-2211". What is the benefit of creating missing months as character strings? What would you do with these character strings once you have them? Please explain how these character strings will be used in future analysis or reporting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I say all of this because it seems to me that there are better ways of getting from here to there (better in the sense of easier programming) that we can help you with, if only we knew where you are trying to go.&amp;nbsp;This seems like a textbook example of the &lt;A href="https://en.wikipedia.org/wiki/XY_problem" target="_self"&gt;XY Problem&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2023 12:13:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-for-each-customer-missing-months/m-p/879509#M347452</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-06-07T12:13:49Z</dc:date>
    </item>
    <item>
      <title>Re: Find for each customer missing months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-for-each-customer-missing-months/m-p/879514#M347456</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input CustID YYMM;
cards;
111 2201
111 2202
111 2203
111 2204
222 2203
222 2204
222 2206
222 2207
222 2208
222 2209
222 2210
333 2206
333 2207
444 2205
444 2207
444 2208
444 2209
444 2210
444 2212
;
Run;
data temp;
 set have;
date=mdy(mod(YYMM,100),1,int(YYMM/100));
format date yymmn4.;
drop YYMM;
run;
proc summary data=temp ;
by CustID;
var date;
output out=temp2(drop=_:) min=min max=max;
run;
data temp3;
 set temp2;
do date=min to max;
 if month ne month(date) then output;
 month=month(date);
end;
format date yymmn4.;
drop min max month;
run;
proc sql;
create table want as
select distinct CustID,put(date,yymmn4. -l) as yymm  from (select * from temp3 except select * from temp)
union
select distinct CustID,'No' from temp where CustID not in (select CustID from (select * from temp3 except select * from temp))
;
quit;
data final_want;
 do until(last.CustID);
  set want;
  by CustID;
 length missing_Months_Vector $ 200;
 missing_Months_Vector=catx(',',missing_Months_Vector,yymm);
 end;
 drop yymm;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Jun 2023 11:33:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-for-each-customer-missing-months/m-p/879514#M347456</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-06-07T11:33:55Z</dc:date>
    </item>
    <item>
      <title>Re: Find for each customer missing months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-for-each-customer-missing-months/m-p/879546#M347477</link>
      <description>&lt;P&gt;See this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input CustID YYMM :yymmn6.;
format yymm yymmn6.;
cards;
111 2201
111 2202
111 2203
111 2204
222 2203
222 2204
222 2206
222 2207
222 2208
222 2209
222 2210
333 2206
333 2207
444 2205
444 2207
444 2208
444 2209
444 2210
444 2212
;

data want;
merge
  have
  have (
    firstobs=2
    rename=(custid=_cust yymm=_yymm)
  )
;
retain n 1;
if _cust = custid
then do;
  yymm = intnx('month',yymm,1,'b');
  do while (yymm lt _yymm);
    output;
    yymm = intnx('month',yymm,1,'b');
    n + 1;
  end;
end;
else n = 1;
keep custid yymm n;
run;

proc report data=want;
column custid yymm,n;
define custid / group;
define yymm / "" analysis;
define n / "" across ;
run;

data _null_;
set want;
by custid;
if first.custid
then put custid yymm @@;
else put +(-1)"," yymm @@;
if last.custid then put;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;One step creates a report, the other similar output which you can write to a file or an ODS destination.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2023 13:59:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-for-each-customer-missing-months/m-p/879546#M347477</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-06-07T13:59:22Z</dc:date>
    </item>
  </channel>
</rss>

