<?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 do I insert a new record after each record with conditions? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-insert-a-new-record-after-each-record-with-conditions/m-p/926025#M364397</link>
    <description>&lt;P&gt;I might suggest something like:&lt;/P&gt;
&lt;PRE&gt;data want ;
    set have (rename=(text3=oldtext));
    /* this will create new text3 variable the length of text4*/
    text3= text4;
    /* set new variable to values of Text3 coming in for your output*/
    text3=oldtext ;
    output;
     if not missing(text2) then do;
         text1=text2;
         text3=text4;
         other=' ';
         count=.;
         output;
    end;
    drop oldtext;
run; &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are combining text from the old Test3 and Text4 then use Text3= oldtext || text4; to create variable the length of text3 and text4 combined to hold more text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't show any attempt to change the length of Text3 so I have to assume it was after the SET statement. Once the variable is in the data vector you can't change the length or the type. If you expect to read a variable from data set on the SET statement and need to change the length you need to do so before the Set:&lt;/P&gt;
&lt;P&gt;This will set the length of the variable Somevar to 15 regardless of the length in the data set Old.&lt;/P&gt;
&lt;PRE&gt;data new;
     length somevar $ 15;
    set old;
run;&lt;/PRE&gt;
&lt;P&gt;The log will warn you if you specify a length shorter than existing that data may be truncated.&lt;/P&gt;</description>
    <pubDate>Fri, 26 Apr 2024 14:43:11 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2024-04-26T14:43:11Z</dc:date>
    <item>
      <title>How do I insert a new record after each record with conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-insert-a-new-record-after-each-record-with-conditions/m-p/925614#M364245</link>
      <description>&lt;P&gt;I want to insert new rows below each record, where the text in one column (in my example Text2) is not null.&amp;nbsp; If it is null, then no inserting of a row.&amp;nbsp; In the inserted row, I want to copy the values of the first 2 grouping variables from the record, insert the text of "Text2" in the field of "Text1", and set the last 2 columns as null and .&amp;nbsp; (null numeric) respectively.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry for the dummy data, but here is what I have:&lt;/P&gt;
&lt;TABLE width="496"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;Group&lt;/TD&gt;
&lt;TD width="64"&gt;Class&lt;/TD&gt;
&lt;TD width="176"&gt;Text1&lt;/TD&gt;
&lt;TD width="64"&gt;Text2&lt;/TD&gt;
&lt;TD width="64"&gt;other1&lt;/TD&gt;
&lt;TD width="64"&gt;count&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;Text for Record 1&lt;/TD&gt;
&lt;TD&gt;text1&lt;/TD&gt;
&lt;TD&gt;AAAA&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;Text for Record 2&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;CCCC&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;B&lt;/TD&gt;
&lt;TD&gt;Text for Record 3&lt;/TD&gt;
&lt;TD&gt;text3&lt;/TD&gt;
&lt;TD&gt;EEEE&lt;/TD&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;C&lt;/TD&gt;
&lt;TD&gt;Text for Record 4&lt;/TD&gt;
&lt;TD&gt;text4&lt;/TD&gt;
&lt;TD&gt;GGGG&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is what I want (inserted records in bold):&lt;/P&gt;
&lt;TABLE width="432"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;Group&lt;/TD&gt;
&lt;TD width="64"&gt;Class&lt;/TD&gt;
&lt;TD width="176"&gt;Text1&lt;/TD&gt;
&lt;TD width="64"&gt;other1&lt;/TD&gt;
&lt;TD width="64"&gt;count&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;Text for Record 1&lt;/TD&gt;
&lt;TD&gt;AAAA&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;A&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;text1&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;.&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;Text for Record 2&lt;/TD&gt;
&lt;TD&gt;CCCC&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;B&lt;/TD&gt;
&lt;TD&gt;Text for Record 3&lt;/TD&gt;
&lt;TD&gt;EEEE&lt;/TD&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;B&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;text3&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;.&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;C&lt;/TD&gt;
&lt;TD&gt;Text for Record 4&lt;/TD&gt;
&lt;TD&gt;GGGG&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;C&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;text4&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;.&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Wed, 24 Apr 2024 17:50:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-insert-a-new-record-after-each-record-with-conditions/m-p/925614#M364245</guid>
      <dc:creator>RandoDando</dc:creator>
      <dc:date>2024-04-24T17:50:06Z</dc:date>
    </item>
    <item>
      <title>Re: How do I insert a new record after each record with conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-insert-a-new-record-after-each-record-with-conditions/m-p/925627#M364248</link>
      <description>&lt;P&gt;UNTESTED CODE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    output;
     if not missing(text2) then do;
         text1=text2;
         other=' ';
         count=.;
         output;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want tested code, please from now on provide the data as working SAS data step code (&lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;examples and instructions&lt;/A&gt;).&lt;/P&gt;</description>
      <pubDate>Wed, 24 Apr 2024 18:28:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-insert-a-new-record-after-each-record-with-conditions/m-p/925627#M364248</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-04-24T18:28:32Z</dc:date>
    </item>
    <item>
      <title>Re: How do I insert a new record after each record with conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-insert-a-new-record-after-each-record-with-conditions/m-p/925630#M364249</link>
      <description>That did it! Thanks!</description>
      <pubDate>Wed, 24 Apr 2024 19:03:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-insert-a-new-record-after-each-record-with-conditions/m-p/925630#M364249</guid>
      <dc:creator>RandoDando</dc:creator>
      <dc:date>2024-04-24T19:03:33Z</dc:date>
    </item>
    <item>
      <title>Re: How do I insert a new record after each record with conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-insert-a-new-record-after-each-record-with-conditions/m-p/926010#M364388</link>
      <description>&lt;P&gt;While that worked, I've hit a snag.&amp;nbsp; I decided to set a text variable in the data set equal to another text variable for the inserted rows.&amp;nbsp; Text3 is the original column I am inserting the text of Text4 into for the inserted rows.&amp;nbsp; However, since the text for Text4 is longer for some cases it's getting truncated despite my efforts to set the length of each to something which can accommodate both.&amp;nbsp; It seems that every character in Text4 beyond the max length used in Text3 isn't showing, despite the length being high enough.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    output;
     if not missing(text2) then do;
         text1=text2;
         text3=text4;
         other=' ';
         count=.;
         output;
    end;
run;4&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2024 13:48:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-insert-a-new-record-after-each-record-with-conditions/m-p/926010#M364388</guid>
      <dc:creator>RandoDando</dc:creator>
      <dc:date>2024-04-26T13:48:07Z</dc:date>
    </item>
    <item>
      <title>Re: How do I insert a new record after each record with conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-insert-a-new-record-after-each-record-with-conditions/m-p/926013#M364390</link>
      <description>&lt;P&gt;Figured out a trick.&amp;nbsp; Instead of using the Length=x statement in PROC SQL I just used a case statement with a nonsense condition.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Case when Text3 = "XYZ" then "                                    " else Text3 end as&amp;nbsp;Text3&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Apr 2024 13:56:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-insert-a-new-record-after-each-record-with-conditions/m-p/926013#M364390</guid>
      <dc:creator>RandoDando</dc:creator>
      <dc:date>2024-04-26T13:56:23Z</dc:date>
    </item>
    <item>
      <title>Re: How do I insert a new record after each record with conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-insert-a-new-record-after-each-record-with-conditions/m-p/926019#M364393</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/264750"&gt;@RandoDando&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Figured out a trick.&amp;nbsp; Instead of using the Length=x statement in PROC SQL I just used a case statement with a nonsense condition.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Case when Text3 = "XYZ" then "                                    " else Text3 end as&amp;nbsp;Text3&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That does not look like a nonsense condition.&amp;nbsp; If by that you mean a condition that is always false you would use 1=0 or in SAS logic just 0 since SAS will treat the number zero as FALSE.&lt;/P&gt;
&lt;P&gt;And for your stated example of inserting values of TEXT4 you could just reference to it directly to make sure the new variable has proper length.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;case when 1=0 then ' ' else text4 as text3&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2024 14:03:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-insert-a-new-record-after-each-record-with-conditions/m-p/926019#M364393</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-04-26T14:03:35Z</dc:date>
    </item>
    <item>
      <title>Re: How do I insert a new record after each record with conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-insert-a-new-record-after-each-record-with-conditions/m-p/926025#M364397</link>
      <description>&lt;P&gt;I might suggest something like:&lt;/P&gt;
&lt;PRE&gt;data want ;
    set have (rename=(text3=oldtext));
    /* this will create new text3 variable the length of text4*/
    text3= text4;
    /* set new variable to values of Text3 coming in for your output*/
    text3=oldtext ;
    output;
     if not missing(text2) then do;
         text1=text2;
         text3=text4;
         other=' ';
         count=.;
         output;
    end;
    drop oldtext;
run; &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are combining text from the old Test3 and Text4 then use Text3= oldtext || text4; to create variable the length of text3 and text4 combined to hold more text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't show any attempt to change the length of Text3 so I have to assume it was after the SET statement. Once the variable is in the data vector you can't change the length or the type. If you expect to read a variable from data set on the SET statement and need to change the length you need to do so before the Set:&lt;/P&gt;
&lt;P&gt;This will set the length of the variable Somevar to 15 regardless of the length in the data set Old.&lt;/P&gt;
&lt;PRE&gt;data new;
     length somevar $ 15;
    set old;
run;&lt;/PRE&gt;
&lt;P&gt;The log will warn you if you specify a length shorter than existing that data may be truncated.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2024 14:43:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-insert-a-new-record-after-each-record-with-conditions/m-p/926025#M364397</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-04-26T14:43:11Z</dc:date>
    </item>
  </channel>
</rss>

