<?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: Add rows per customer ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Add-rows-per-customer-ID/m-p/848491#M335456</link>
    <description>&lt;P&gt;Do you have SAS/ETS?&lt;/P&gt;</description>
    <pubDate>Thu, 08 Dec 2022 09:27:27 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2022-12-08T09:27:27Z</dc:date>
    <item>
      <title>Add rows per customer ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-rows-per-customer-ID/m-p/848484#M335455</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;For each customer there are multiple rows (Each row has information per specific month).&lt;/P&gt;
&lt;P&gt;Data set t1 contain information of wealth per customer ID + month .&lt;/P&gt;
&lt;P&gt;Data set t2 contain list of months .&lt;/P&gt;
&lt;P&gt;Task: I want to add for each customer rows for months that have no data.&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;ID=1 have no information of months:2205,2206&amp;nbsp; so need to add 2 rows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the way to create wanted data set?&lt;/P&gt;
&lt;P&gt;Please see the way I did it and giving the requested results,&lt;/P&gt;
&lt;P&gt;I want to ask if there is a shorter and more efficient way?&lt;/P&gt;
&lt;P&gt;Please note that in real life t1 data set is very big (50 million rows and 20 columns)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data t1;
input ID  month wealth;
cards;
1 2201 10
1 2202 20
1 2203 30
1 2204 40
2 2201 50
2 2202 60
2 2203 70
2 2204 80
2 2205 90
2 2206 100
3 2201 110
3 2202 120
3 2204 130
3 2205 140
3 2206 150
;
run;


Data t2;
input month_;
cards;
2201
2202
2203
2204
2205
2206
;
Run;



proc sql;
create table t3 as
select a.*,b.month_ 
from t1 as a,t2  as b
order by ID,month,month_
;
quit;

Data t4;
set t3;
IF month ne month_ then wealth=.;
Run;

data part1(drop=month_);
set t4(where=(month=month_));
Run;

data part2;
set t4(where=(month ne month_));
Run;
proc sql;
create table part2b as
select  distinct ID,month_
from part2
;
quit;
proc sql;
create table part2c(rename=(month_=month)) as
select a.*
from part2b as a
left join part1 as b
on a.month_=b.month and a.ID=b.ID
where b.ID is null
;
quit;

proc sort data=part1;by ID;Run;
proc sort data=part2c;by ID;Run;

Data want;
SET  part1 part2c;
by ID;
Run;
proc sort data=want;by ID month;Run;



 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 09:10:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-rows-per-customer-ID/m-p/848484#M335455</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-12-08T09:10:55Z</dc:date>
    </item>
    <item>
      <title>Re: Add rows per customer ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-rows-per-customer-ID/m-p/848491#M335456</link>
      <description>&lt;P&gt;Do you have SAS/ETS?&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 09:27:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-rows-per-customer-ID/m-p/848491#M335456</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-12-08T09:27:27Z</dc:date>
    </item>
    <item>
      <title>Re: Add rows per customer ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-rows-per-customer-ID/m-p/848493#M335457</link>
      <description>SAS, what is ETS??</description>
      <pubDate>Thu, 08 Dec 2022 09:39:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-rows-per-customer-ID/m-p/848493#M335457</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-12-08T09:39:42Z</dc:date>
    </item>
    <item>
      <title>Re: Add rows per customer ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-rows-per-customer-ID/m-p/848496#M335458</link>
      <description>&lt;P&gt;ETS is the license that gives you access to Proc Timeseries and Proc Expand and so on.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 10:35:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-rows-per-customer-ID/m-p/848496#M335458</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-12-08T10:35:49Z</dc:date>
    </item>
    <item>
      <title>Re: Add rows per customer ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-rows-per-customer-ID/m-p/848497#M335459</link>
      <description>How can I check if I have ETS?</description>
      <pubDate>Thu, 08 Dec 2022 10:45:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-rows-per-customer-ID/m-p/848497#M335459</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-12-08T10:45:21Z</dc:date>
    </item>
    <item>
      <title>Re: Add rows per customer ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-rows-per-customer-ID/m-p/848498#M335460</link>
      <description>&lt;P&gt;Run&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc setinit;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and check the log &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 10:46:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-rows-per-customer-ID/m-p/848498#M335460</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-12-08T10:46:28Z</dc:date>
    </item>
    <item>
      <title>Re: Add rows per customer ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-rows-per-customer-ID/m-p/848523#M335464</link>
      <description>&lt;P&gt;Next code is tested and will do the work, supposed that t2 is not too big.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data t2;
input month_;
cards;
2201
2202
2203
2204
2205
2206
;
Run;
proc sql noprint; 
  select month_ into : months separated by ','
  from t2;
run;

data _null_;
  mm = "&amp;amp;months";
  nm = countw(mm);
  call symput('nm',compress(put(nm,2.)));
run;

%put nm=&amp;amp;nm months = &amp;amp;months;

Data t1;
input ID  month wealth;
cards;
1 2201 10
1 2202 20
1 2203 30
1 2204 40
2 2201 50
2 2202 60
2 2203 70
2 2204 80
2 2205 90
2 2206 100
3 2201 110
3 2202 120
3 2204 130
3 2205 140
3 2206 150
;
run;

data want;
   set t1;
    by ID;
       retain m1-m&amp;amp;nm mmx1-mmx&amp;amp;nm;
	   array mmx {&amp;amp;nm} (&amp;amp;months) ;
	   array mm {&amp;amp;nm} m1-m&amp;amp;nm;
	   drop i m1-m&amp;amp;nm mmx1-mmx&amp;amp;nm;
	
       if first.ID then do i=1 to &amp;amp;nm; mm(i)=.; end; 
       if month in mmx then do;  
          i=1; 
          do while (i le &amp;amp;nm); 
             if mmx(i) = month then do;
                put _N_= ID= i= month= ;
                mm(i)=1;
                leave;
             end;
             i+1; 
          end;   
          output;
       end;
       if last.ID then do;
          do i=1 to &amp;amp;nm;
             if mm(i) =. then do;
             wealth=.; 
             month = mmx(i);
             output;
             end;
          end;
       end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You may want to add a sort of the result file by ID month;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 11:56:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-rows-per-customer-ID/m-p/848523#M335464</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2022-12-08T11:56:59Z</dc:date>
    </item>
    <item>
      <title>Re: Add rows per customer ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-rows-per-customer-ID/m-p/848537#M335472</link>
      <description>&lt;P&gt;This is as simple as I could make it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set t1;
  by id;
  if first.id then _P_=0;
  do _P_=_P_+1 to _months until(month&amp;gt;=month_);
    set t2 point=_P_ nobs=_months;
    output;
    end;
  if last.id;
  do _P_=_P_+1 to _months;
    set t2 point=_P_ nobs=_months;
    output;
    end;
  keep ID month_ wealth;
  rename month_=month;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Dec 2022 14:35:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-rows-per-customer-ID/m-p/848537#M335472</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-12-08T14:35:20Z</dc:date>
    </item>
    <item>
      <title>Re: Add rows per customer ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-rows-per-customer-ID/m-p/849783#M335916</link>
      <description>&lt;P&gt;You can try this:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table t_1 as&lt;BR /&gt;select *&lt;BR /&gt;from t2&lt;/P&gt;
&lt;P&gt;cross join (Select distinct Id from t1)&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table t_2 as&lt;BR /&gt;select a.Id, a.month_ , b.wealth&lt;BR /&gt;from t_1 as a&lt;BR /&gt;left join t1 as b&lt;BR /&gt;on a.month_=b.month and a.id=b.Id&lt;BR /&gt;order by a.ID, a.month_&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt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;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;2204&lt;/TD&gt;
&lt;TD&gt;80&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;2205&lt;/TD&gt;
&lt;TD&gt;90&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;2206&lt;/TD&gt;
&lt;TD&gt;100&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;2201&lt;/TD&gt;
&lt;TD&gt;110&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;2202&lt;/TD&gt;
&lt;TD&gt;120&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;2203&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;2204&lt;/TD&gt;
&lt;TD&gt;130&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;2205&lt;/TD&gt;
&lt;TD&gt;140&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;2206&lt;/TD&gt;
&lt;TD&gt;150&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Thu, 15 Dec 2022 05:21:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-rows-per-customer-ID/m-p/849783#M335916</guid>
      <dc:creator>SK_11</dc:creator>
      <dc:date>2022-12-15T05:21:05Z</dc:date>
    </item>
  </channel>
</rss>

