<?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 substring to character variable (participant ID) for rows missing leading characters in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Add-substring-to-character-variable-participant-ID-for-rows/m-p/802066#M315712</link>
    <description>&lt;P&gt;Below should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input ID :$char11.;
  datalines;
2
  6
8
 12
13
14
20
21
358
439
589
ABCDEFG0001
ABCDEFG0003
;

data want;
  set have;
  if put(input(id,?? 11.),11.-l)=strip(id) then 
    do;
      id=cats('ABCDEFG',put(input(id,11.),z4.));
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1647292592308.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/69458iE67E49BECE86E444/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1647292592308.png" alt="Patrick_0-1647292592308.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 14 Mar 2022 21:16:41 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2022-03-14T21:16:41Z</dc:date>
    <item>
      <title>Add substring to character variable (participant ID) for rows missing leading characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-substring-to-character-variable-participant-ID-for-rows/m-p/802061#M315710</link>
      <description>&lt;P&gt;I have a dataset where the participant ID is 11 characters long and starts with the letters ABCDEFG followed by 4 numbers. Current variable format is $CHAR11. The IDs count up by one beginning with 1, so ABCDEFG0001, ABCDEFG002....ABCDEFG0875, etc. However, some of the IDs were entered in error without the letters and leading zeros (see below). How do I add the letters and correct number of leading zeros to the rows where they are missing while leaving the properly formatted IDs as-is?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;ID&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;14&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;21&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;358&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;439&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;589&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ABCDEFG0001&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ABCDEFG0003&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Mon, 14 Mar 2022 21:01:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-substring-to-character-variable-participant-ID-for-rows/m-p/802061#M315710</guid>
      <dc:creator>lh50</dc:creator>
      <dc:date>2022-03-14T21:01:37Z</dc:date>
    </item>
    <item>
      <title>Re: Add substring to character variable (participant ID) for rows missing leading characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-substring-to-character-variable-participant-ID-for-rows/m-p/802066#M315712</link>
      <description>&lt;P&gt;Below should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input ID :$char11.;
  datalines;
2
  6
8
 12
13
14
20
21
358
439
589
ABCDEFG0001
ABCDEFG0003
;

data want;
  set have;
  if put(input(id,?? 11.),11.-l)=strip(id) then 
    do;
      id=cats('ABCDEFG',put(input(id,11.),z4.));
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1647292592308.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/69458iE67E49BECE86E444/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1647292592308.png" alt="Patrick_0-1647292592308.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Mar 2022 21:16:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-substring-to-character-variable-participant-ID-for-rows/m-p/802066#M315712</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-03-14T21:16:41Z</dc:date>
    </item>
    <item>
      <title>Re: Add substring to character variable (participant ID) for rows missing leading characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-substring-to-character-variable-participant-ID-for-rows/m-p/802069#M315715</link>
      <description>This worked great, thanks. One caveat I forgot to mention: there are also some IDs that are a string of 7 or 8 numbers that don't follow the numerical pattern (1549817 for example). Is there any way to correct the IDs as you did in your solution while leaving these unchanged? When I apply your solution, it makes these IDs look like ABCDEFG17E7 (using the above example). I would rather leave those IDs unchanged if possible.</description>
      <pubDate>Mon, 14 Mar 2022 21:32:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-substring-to-character-variable-participant-ID-for-rows/m-p/802069#M315715</guid>
      <dc:creator>lh50</dc:creator>
      <dc:date>2022-03-14T21:32:09Z</dc:date>
    </item>
    <item>
      <title>Re: Add substring to character variable (participant ID) for rows missing leading characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-substring-to-character-variable-participant-ID-for-rows/m-p/802071#M315717</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/320622"&gt;@lh50&lt;/a&gt;&amp;nbsp;You could exclude any string longer than n-characters.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  id=strip(id);
  if length(id)&amp;lt;=11-length('ABCDEFG') then 
    do;
      if put(input(id,?? 11.),11.-l)=strip(id) then 
        do;
          id=cats('ABCDEFG',put(input(id,11.),z4.));
        end;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Mar 2022 21:42:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-substring-to-character-variable-participant-ID-for-rows/m-p/802071#M315717</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-03-14T21:42:40Z</dc:date>
    </item>
    <item>
      <title>Re: Add substring to character variable (participant ID) for rows missing leading characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-substring-to-character-variable-participant-ID-for-rows/m-p/802076#M315721</link>
      <description>&lt;P&gt;One way that seems to work with the example:&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   select (length (id));
      when (1,2,3,4) Id=cats('ABCDEFG',put(input(id,4.),z4.));
      when (11); /*nothing to do*/
      otherwise put "WARNING: Rule for handling this case not provided " id=;
   end;
run;&lt;/PRE&gt;
&lt;P&gt;The Othewise would execute when you have an unexpected number of characters such as 5. Just in case, this puts a warning in the log.&lt;/P&gt;
&lt;P&gt;The Select statement evaluates the code in the parentheses and finds the value returned, in this case by the Length function and will execute the statement following the When with the result. 1 through 4 are expected to be up to a 4 digit number. 11 expects to be a complete Id with no need to fix. Any other result is likely a bit more complicated to fix, if they occur. Such as a number like 12345. No zeroes, too many digits for your rule so likely needs manual intervention.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Mar 2022 22:01:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-substring-to-character-variable-participant-ID-for-rows/m-p/802076#M315721</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-03-14T22:01:29Z</dc:date>
    </item>
    <item>
      <title>Re: Add substring to character variable (participant ID) for rows missing leading characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-substring-to-character-variable-participant-ID-for-rows/m-p/802246#M315811</link>
      <description>Works perfectly. Thank you!</description>
      <pubDate>Tue, 15 Mar 2022 15:14:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-substring-to-character-variable-participant-ID-for-rows/m-p/802246#M315811</guid>
      <dc:creator>lh50</dc:creator>
      <dc:date>2022-03-15T15:14:55Z</dc:date>
    </item>
  </channel>
</rss>

