<?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: How to insert rows by maximum number in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/665106#M198798</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/304931"&gt;@superbug&lt;/a&gt;&amp;nbsp; Try this. It works with the data you provided.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input item_id $ resp;
    datalines;
    PP1111 1
    PP1111 2
    PP1111 3
    pp2222 1
    pp2222 2
    pp2222 3
    pp2222 4
    pp3333 1
    pp3333 3
    pp3333 4
    pp4444 1
    pp4444 2
    pp5555 1
    ;
run;

proc sql noprint;
  select substr(max(item_id),3,1) into: max from have;
quit;

data arr (keep=a: i);
    array arr{&amp;amp;max.,4};
    set have;
    i = input(substr(item_id,3,1), 1.);
    arr{i,resp} = resp;
run;

proc transpose data=arr out=arr_trans name=arr_num;
    by i;
run;

data want (keep=item_id resp);
    set arr_trans;
    var = coalesce(col1, col2, col3, col4);
    item_id = cat('pp', put(i,1.), put(i,1.), put(i,1.), put(i,1.));
    resp = var;
    where (arr_num = "arr"||strip(put(((i-1)*4+1),2.))
        or arr_num = "arr"||strip(put(((i-1)*4+2),2.))
        or arr_num = "arr"||strip(put(((i-1)*4+3),2.))
        or arr_num = "arr"||strip(put(((i-1)*4+4),2.)));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This code could certainly be cleaned up a lot. This is not a very dynamic approach. I have no doubt there is a cleaner solution.&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Edited to remove unnecessary code and reduce hard-coding.&lt;/EM&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 25 Jun 2020 18:28:20 GMT</pubDate>
    <dc:creator>mklangley</dc:creator>
    <dc:date>2020-06-25T18:28:20Z</dc:date>
    <item>
      <title>How to insert rows by maximum number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/664827#M198681</link>
      <description>&lt;P&gt;My data&amp;nbsp; is as attached. The left two columns is the data format I have, the right two columns is the data format I want. The rows highlighted in red are the rows I want to insert.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Each "item_id"&lt;STRONG&gt; is supposed&lt;/STRONG&gt; to have four rows, but some "item_id" only have three, two, or one rows. How to reformat the data I have to the data I want?&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 02:14:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/664827#M198681</guid>
      <dc:creator>superbug</dc:creator>
      <dc:date>2020-06-25T02:14:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert rows by maximum number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/664856#M198684</link>
      <description>&lt;P&gt;Did you know that, instead of attaching a spreadsheet that each of your potential helpers would need to download and open, you can paste its contents directly into a table in your topic statement?&amp;nbsp; Then we could see the values instantly and focus our effort on helping to find a solution instead of figuring out the question.&amp;nbsp; Encourage us.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 03:56:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/664856#M198684</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-06-25T03:56:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert rows by maximum number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/664870#M198686</link>
      <description>&lt;P&gt;Or, even better than posting data as table, post it as a data-step using datalines, so that we actually have data, not a spreadsheet.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 04:58:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/664870#M198686</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-06-25T04:58:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert rows by maximum number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/664875#M198688</link>
      <description>&lt;P&gt;Please post example data in self-contained data steps, as you have been shown repeatedly in your previous threads. It's not rocket science, and it helps us greatly in helping you.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 05:32:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/664875#M198688</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-06-25T05:32:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert rows by maximum number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/664966#M198734</link>
      <description>Did you try &lt;BR /&gt;Data want(&lt;BR /&gt;Set a b;&lt;BR /&gt;By item_id;&lt;BR /&gt;Run;&lt;BR /&gt;In sas terminology this is interleaving.</description>
      <pubDate>Thu, 25 Jun 2020 11:31:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/664966#M198734</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-06-25T11:31:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert rows by maximum number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/664998#M198756</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214340"&gt;@smantha&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for the suggestion of how to post effective questions! I am sorry for any inconveniences of all my previous posts. The picture below is the data I am having and the format I want. I hope the picture can be self-explaining.&lt;/P&gt;
&lt;P&gt;The data format I am having is like the A and B columns. Basically, each individual "item_id" is supposed to have four rows. If the rows of each individual "item_id" is less than four,&amp;nbsp; I want to make them to be four rows, like columns D and E. In column D, highlights in&lt;FONT color="#FF0000"&gt; red&lt;/FONT&gt; is the rows that I want to insert.&lt;/P&gt;
&lt;P&gt;Thanks for the help!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="data photo.PNG" style="width: 381px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/46648i0F11AE08F3A4F540/image-size/large?v=v2&amp;amp;px=999" role="button" title="data photo.PNG" alt="data photo.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 13:07:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/664998#M198756</guid>
      <dc:creator>superbug</dc:creator>
      <dc:date>2020-06-25T13:07:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert rows by maximum number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/665036#M198772</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/304931"&gt;@superbug&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just to be clear.&amp;nbsp; You posted a picture, not a table, or (gold standard per &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;) a data step&amp;nbsp;reading raw data.&amp;nbsp; The png is a nice start, but don't stop there.&amp;nbsp; After all, the proposed solution showed a generic program structure, but somebody will have to modify it to use the actual data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Help us help you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 14:54:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/665036#M198772</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-06-25T14:54:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert rows by maximum number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/665106#M198798</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/304931"&gt;@superbug&lt;/a&gt;&amp;nbsp; Try this. It works with the data you provided.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input item_id $ resp;
    datalines;
    PP1111 1
    PP1111 2
    PP1111 3
    pp2222 1
    pp2222 2
    pp2222 3
    pp2222 4
    pp3333 1
    pp3333 3
    pp3333 4
    pp4444 1
    pp4444 2
    pp5555 1
    ;
run;

proc sql noprint;
  select substr(max(item_id),3,1) into: max from have;
quit;

data arr (keep=a: i);
    array arr{&amp;amp;max.,4};
    set have;
    i = input(substr(item_id,3,1), 1.);
    arr{i,resp} = resp;
run;

proc transpose data=arr out=arr_trans name=arr_num;
    by i;
run;

data want (keep=item_id resp);
    set arr_trans;
    var = coalesce(col1, col2, col3, col4);
    item_id = cat('pp', put(i,1.), put(i,1.), put(i,1.), put(i,1.));
    resp = var;
    where (arr_num = "arr"||strip(put(((i-1)*4+1),2.))
        or arr_num = "arr"||strip(put(((i-1)*4+2),2.))
        or arr_num = "arr"||strip(put(((i-1)*4+3),2.))
        or arr_num = "arr"||strip(put(((i-1)*4+4),2.)));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This code could certainly be cleaned up a lot. This is not a very dynamic approach. I have no doubt there is a cleaner solution.&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Edited to remove unnecessary code and reduce hard-coding.&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 18:28:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/665106#M198798</guid>
      <dc:creator>mklangley</dc:creator>
      <dc:date>2020-06-25T18:28:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert rows by maximum number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/665118#M198805</link>
      <description>&lt;P&gt;Is it always 4 observations per id?&amp;nbsp; Does the value of the other variable matter?&lt;/P&gt;
&lt;P&gt;The classic way to do this is to build a template dataset and then overlay your actual values.&amp;nbsp; &amp;nbsp;The CALL MISSING() handles side effects of many-to-many merge.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data template ;
  set have ;
  by id ;
  if first.id then do i=1 to 4;
    output;
  end;
  keep id;
run;
data want;
  merge template have ;
  by id ;
  output;
  call missing(of _all_);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If your extra variable is really a sequence counter then it is easier as you don't have to deal with many-to-many merge.&amp;nbsp; Plus it will properly insert empty rows that are not the last in the group, but are instead somewhere in the middle.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id seq value ;
cards;
1 1 23
1  2 123
2  1 45
2  3  23
;
data template;
   set have ;
   by id ;
   if first.id then do seq=1 to 4;
       output;
   end;
  keep id seq;
run;
data want ;
  merge template have;
  by id seq ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 18:39:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/665118#M198805</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-06-25T18:39:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert rows by maximum number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/665133#M198814</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much, your code is neat and easily understand.&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/223296"&gt;@mklangley&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you as well!&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 19:33:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-insert-rows-by-maximum-number/m-p/665133#M198814</guid>
      <dc:creator>superbug</dc:creator>
      <dc:date>2020-06-25T19:33:51Z</dc:date>
    </item>
  </channel>
</rss>

