<?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 separate part of an alphanumeric string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-separate-part-of-an-alphanumeric-string/m-p/853869#M337484</link>
    <description>&lt;P&gt;You can try something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  length sid $13;
  sid=scan(ID,2,'D');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That will work if you always have 13 digits after the "D". If the digit string is sometimes shorter (so that you get the hyphen and possibly other stuff in the SID variable, you could change it to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  length sid $13;
  sid=scan(scan(ID,2,'D'),1,'-');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 14 Jan 2023 11:00:17 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2023-01-14T11:00:17Z</dc:date>
    <item>
      <title>How to separate part of an alphanumeric string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-separate-part-of-an-alphanumeric-string/m-p/853827#M337463</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to know how I can separate part of a variable. The values for the variable named ID look like this: AAID1248987441122-998-IDSID-0-1. I need to only extract the 13 digits after the letter D and before the first hyphen (i.e.,&amp;nbsp;1248987441122. the new variable name will be sid). I have tried scan and compress but neither produces what I need.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;sid = scan(ID,4) results in a value of only 1&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;sid = compress(ID, """,'A') results in a value like this&amp;nbsp;1248987441122-998-0-1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your help would be greatly appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2023 22:57:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-separate-part-of-an-alphanumeric-string/m-p/853827#M337463</guid>
      <dc:creator>Sam20001</dc:creator>
      <dc:date>2023-01-13T22:57:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to separate part of an alphanumeric string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-separate-part-of-an-alphanumeric-string/m-p/853831#M337466</link>
      <description>SID = compress(scan(ID, 1, "-"), , 'kd');&lt;BR /&gt;&lt;BR /&gt;Use both COMPRESS + SCAN. &lt;BR /&gt;&lt;BR /&gt;SCAN to get the first portion of the text and COMPRESS to remove the first 4 characters. &lt;BR /&gt;&lt;BR /&gt;Or if the text is always a fixed length use SUBSTR. &lt;BR /&gt;&lt;BR /&gt;SID = substr(ID, 5, 13);</description>
      <pubDate>Fri, 13 Jan 2023 23:17:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-separate-part-of-an-alphanumeric-string/m-p/853831#M337466</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-01-13T23:17:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to separate part of an alphanumeric string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-separate-part-of-an-alphanumeric-string/m-p/853832#M337467</link>
      <description>&lt;P&gt;One or both of below two options should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample;
  infile datalines truncover;
  input have :$31.;
  length want1 want2 $13;
  want1=scan(scan(have,1,'-'),-2,'kd');
  /* if the wanted digits are always on the same position and it's always 13 digits */ 
  /*  then substr() will work as well */
  want2=substr(have,5,13);
  datalines;
AAID1248987441122-998-IDSID-0-1
 
AAID99999999-998-IDSID-0-1
;

proc print data=sample;
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-1673665811767.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/79400iC74139DD557EC4E0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1673665811767.png" alt="Patrick_0-1673665811767.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 14 Jan 2023 11:14:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-separate-part-of-an-alphanumeric-string/m-p/853832#M337467</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-01-14T11:14:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to separate part of an alphanumeric string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-separate-part-of-an-alphanumeric-string/m-p/853869#M337484</link>
      <description>&lt;P&gt;You can try something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  length sid $13;
  sid=scan(ID,2,'D');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That will work if you always have 13 digits after the "D". If the digit string is sometimes shorter (so that you get the hyphen and possibly other stuff in the SID variable, you could change it to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  length sid $13;
  sid=scan(scan(ID,2,'D'),1,'-');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 14 Jan 2023 11:00:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-separate-part-of-an-alphanumeric-string/m-p/853869#M337484</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-01-14T11:00:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to separate part of an alphanumeric string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-separate-part-of-an-alphanumeric-string/m-p/853871#M337485</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/430981"&gt;@Sam20001&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could also use the PRXCHANGE function. It is more flexible, so it can be coded to handle many different input formats. The learning courve is a bit steep if you are unfamiliar with the SAS PRX functions, and I recomment the PRX Tip sheet as a great way of getting started: &lt;A href="https://support.sas.com/rnd/base/datastep/perl_regexp/regexp-tip-sheet.pdf" target="_blank"&gt;https://support.sas.com/rnd/base/datastep/perl_regexp/regexp-tip-sheet.pdf&lt;/A&gt;:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your case, the string seems to be machine generated, so a flexible solution is not called for. Other contributors have suggested different solutions that work perfectily well on your example string, so I am just beating the drum over the use of PRX functions; they have saved me the trouble of coding many lines of complicated code over the years. They are known to be ineffctive, but in my experience it's not a problem worth considering unless input is counted in millions of observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length ID $40;
  input ID $char40.;
  datalines;
AAID1248987441122-998-IDSID-0-1
ID1248987441122-998--0-1
1248987441122
123
AAID1-998-IDSID-0-1
AAIN1248987441122_998-IDSID-0-1
AAID-1248987441122DSID-0-1
;
data want;
  set have;
  SID = prxchange('s/(\D+)(\d*)(\D.*)/$2/',-1,ID);

  * other suggestions in this post;
  SID2 = compress(scan(ID, 1, "-"), , 'kd');
  SID3 = scan(scan(ID,1,'-'),-2,'kd');
  SID4 = scan(ID,2,'D');
  SID5 = scan(scan(ID,2,'D'),1,'-');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 14 Jan 2023 12:46:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-separate-part-of-an-alphanumeric-string/m-p/853871#M337485</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2023-01-14T12:46:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to separate part of an alphanumeric string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-separate-part-of-an-alphanumeric-string/m-p/857407#M338800</link>
      <description>&lt;P&gt;All the examples work. Thank you so much for your help with this!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Feb 2023 16:52:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-separate-part-of-an-alphanumeric-string/m-p/857407#M338800</guid>
      <dc:creator>Sam20001</dc:creator>
      <dc:date>2023-02-06T16:52:28Z</dc:date>
    </item>
  </channel>
</rss>

