<?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: Adding one row based on condition per group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Adding-one-row-based-on-condition-per-group/m-p/805377#M317254</link>
    <description>&lt;P&gt;If the "amount" is a numeric variable as shown in your data step you &lt;STRONG&gt;cannot&lt;/STRONG&gt; have a value of "N/A". You can have a missing value. If you want to see a displayed value of "N/A" then you can create format that will do such but the value can't be text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This makes a set that looks like that when the format MYNA is available. You would have to make sure the format code is run in any session you want to see N/A, or learn about format management to create and use formats stored in permanent libraries.&lt;/P&gt;
&lt;PRE&gt;data have;
input
name : $1.
id : 8.
code : $8.
amount : 8.
;

datalines;
A 10 12345678 100
A 10 09876543 30
B 9 88997700 20
B 9 88997700 -20
B 9 88997700 40
C 28 11223344 30
C 28 11223344 40
C 28 67676767 50
D 10 78654890 40
D 10 78654890 50
;

proc format;
value myna
.N='N/A'
;
run;
data want;
   set have;
   by name id;
   length firstcode $ 8;
   retain firstcode;
   if first.id then firstcode=code;
   if last.id and code=firstcode then do;
      /* write the current values to the data set*/
      output;
      /* assign missing to amount*/
      amount = .N;
      output;
   end;
   else output;
   format amount myna. ;
   drop firstcode;
run;   &lt;/PRE&gt;
&lt;P&gt;SAS has 27 "special" missing values from .A to .Z plus ._&amp;nbsp; in addition to the default . missing.&amp;nbsp; You can assign formats to display different meanings for each of them for a single variable. The special missing values will appear as A to Z or _ if a format is not assigned to display them differently.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 31 Mar 2022 18:33:24 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2022-03-31T18:33:24Z</dc:date>
    <item>
      <title>Adding one row based on condition per group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-one-row-based-on-condition-per-group/m-p/805372#M317250</link>
      <description>&lt;P&gt;If I group the data I have by name and id, I want to add a new row per group if the first code = last code, the new row need to have the same name and id, and the value of the 'amount' column is N/A, the values for the rest columns can just be the same as one of the rows in the group (can also just be empty, doesn't really matter).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input&lt;BR /&gt;name : $1.&lt;BR /&gt;id : 8.&lt;BR /&gt;code : $8.&lt;BR /&gt;amount : 8.&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;datalines;&lt;BR /&gt;A 10 12345678 100&lt;BR /&gt;A 10 09876543 30&lt;BR /&gt;B 9 88997700 20&lt;BR /&gt;B 9 88997700 -20&lt;BR /&gt;B 9 88997700 40&lt;BR /&gt;C 28 11223344 30&lt;BR /&gt;C 28 11223344 40&lt;BR /&gt;C 28 67676767 50&lt;/P&gt;&lt;P&gt;D 10 78654890 40&lt;BR /&gt;D 10 78654890 50&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;input&lt;BR /&gt;name : $1.&lt;BR /&gt;id : 8.&lt;BR /&gt;code : $8.&lt;BR /&gt;amount : 8.&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;datalines;&lt;BR /&gt;A 10 12345678 100&lt;BR /&gt;A 10 09876543 30&lt;BR /&gt;B 9 88997700 20&lt;BR /&gt;B 9 88997700 -20&lt;BR /&gt;B 9 88997700 40&lt;/P&gt;&lt;P&gt;B 9 88997700 N/A&lt;BR /&gt;C 28 11223344 30&lt;BR /&gt;C 28 11223344 40&lt;BR /&gt;C 28 67676767 50&lt;/P&gt;&lt;P&gt;D 10 78654890 40&lt;BR /&gt;D 10 78654890 50&lt;/P&gt;&lt;P&gt;D 10 78654890 N/A&lt;BR /&gt;;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Mar 2022 17:45:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-one-row-based-on-condition-per-group/m-p/805372#M317250</guid>
      <dc:creator>noelle12</dc:creator>
      <dc:date>2022-03-31T17:45:51Z</dc:date>
    </item>
    <item>
      <title>Re: Adding one row based on condition per group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-one-row-based-on-condition-per-group/m-p/805377#M317254</link>
      <description>&lt;P&gt;If the "amount" is a numeric variable as shown in your data step you &lt;STRONG&gt;cannot&lt;/STRONG&gt; have a value of "N/A". You can have a missing value. If you want to see a displayed value of "N/A" then you can create format that will do such but the value can't be text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This makes a set that looks like that when the format MYNA is available. You would have to make sure the format code is run in any session you want to see N/A, or learn about format management to create and use formats stored in permanent libraries.&lt;/P&gt;
&lt;PRE&gt;data have;
input
name : $1.
id : 8.
code : $8.
amount : 8.
;

datalines;
A 10 12345678 100
A 10 09876543 30
B 9 88997700 20
B 9 88997700 -20
B 9 88997700 40
C 28 11223344 30
C 28 11223344 40
C 28 67676767 50
D 10 78654890 40
D 10 78654890 50
;

proc format;
value myna
.N='N/A'
;
run;
data want;
   set have;
   by name id;
   length firstcode $ 8;
   retain firstcode;
   if first.id then firstcode=code;
   if last.id and code=firstcode then do;
      /* write the current values to the data set*/
      output;
      /* assign missing to amount*/
      amount = .N;
      output;
   end;
   else output;
   format amount myna. ;
   drop firstcode;
run;   &lt;/PRE&gt;
&lt;P&gt;SAS has 27 "special" missing values from .A to .Z plus ._&amp;nbsp; in addition to the default . missing.&amp;nbsp; You can assign formats to display different meanings for each of them for a single variable. The special missing values will appear as A to Z or _ if a format is not assigned to display them differently.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Mar 2022 18:33:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-one-row-based-on-condition-per-group/m-p/805377#M317254</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-03-31T18:33:24Z</dc:date>
    </item>
  </channel>
</rss>

