<?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: extracting text between commas in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/extracting-text-between-commas/m-p/756610#M238918</link>
    <description>&lt;P&gt;I hope this one help, because you want to replace ',' in string into '|'&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data dataset1;&lt;BR /&gt;input value $ 1-150;&lt;BR /&gt;datalines;&lt;BR /&gt;Press Attache and Country Information Officer, Office of the U.S. Ambassador to Pakistan, U.S. Embassy in Pakistan, United States Department of State&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data dataset2;&lt;BR /&gt;set dataset1;&lt;BR /&gt;value = tranwrd(value,',','|');&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Mon, 26 Jul 2021 10:50:05 GMT</pubDate>
    <dc:creator>Manoharan141</dc:creator>
    <dc:date>2021-07-26T10:50:05Z</dc:date>
    <item>
      <title>extracting text between commas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-text-between-commas/m-p/756561#M238897</link>
      <description>&lt;P&gt;Hi colleagues,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a text variable that has multiple commas -- sometimes two commas, sometimes three commas. I'd like to extract the text between the commas but can't seem to find the right code to do that efficiently. I'm including a few examples of the variables below. Any suggestions will be greatly appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have:&lt;/P&gt;&lt;P&gt;Press Attaché and Country Information Officer, Office of the U.S. Ambassador to Pakistan, U.S. Embassy in Pakistan, United States Department of State&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Want&lt;/P&gt;&lt;P&gt;Press Attache and Country Information Officer | Office of the U.S. Ambassador to Pakistan | U.S. Embassy in Pakistan | United States Department of State&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jul 2021 05:05:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-text-between-commas/m-p/756561#M238897</guid>
      <dc:creator>PavSingh</dc:creator>
      <dc:date>2021-07-26T05:05:57Z</dc:date>
    </item>
    <item>
      <title>Re: extracting text between commas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-text-between-commas/m-p/756565#M238899</link>
      <description>&lt;P&gt;So, you don't want to extract anything, but replace a comma followed by a blank by " | ". Have a look at the function tranwrd, it exists for such tasks.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jul 2021 05:21:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-text-between-commas/m-p/756565#M238899</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-07-26T05:21:15Z</dc:date>
    </item>
    <item>
      <title>Re: extracting text between commas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-text-between-commas/m-p/756610#M238918</link>
      <description>&lt;P&gt;I hope this one help, because you want to replace ',' in string into '|'&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data dataset1;&lt;BR /&gt;input value $ 1-150;&lt;BR /&gt;datalines;&lt;BR /&gt;Press Attache and Country Information Officer, Office of the U.S. Ambassador to Pakistan, U.S. Embassy in Pakistan, United States Department of State&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data dataset2;&lt;BR /&gt;set dataset1;&lt;BR /&gt;value = tranwrd(value,',','|');&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jul 2021 10:50:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-text-between-commas/m-p/756610#M238918</guid>
      <dc:creator>Manoharan141</dc:creator>
      <dc:date>2021-07-26T10:50:05Z</dc:date>
    </item>
    <item>
      <title>Re: extracting text between commas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-text-between-commas/m-p/756681#M238948</link>
      <description>&lt;P&gt;My mistake in detailing my challenge. I'd like to extract the text between the commas, so they land in individual column variables.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jul 2021 15:57:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-text-between-commas/m-p/756681#M238948</guid>
      <dc:creator>PavSingh</dc:creator>
      <dc:date>2021-07-26T15:57:07Z</dc:date>
    </item>
    <item>
      <title>Re: extracting text between commas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-text-between-commas/m-p/756718#M238974</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/390630"&gt;@PavSingh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;My mistake in detailing my challenge. I'd like to extract the text between the commas, so they land in individual column variables.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;One way as an example:&lt;/P&gt;
&lt;PRE&gt;data example;
   x="Press Attaché and Country Information Officer, Office of the U.S. Ambassador to Pakistan, U.S. Embassy in Pakistan, United States Department of State";
   Array v (15) $ 50;
   do i=1 to countw(x,',');
      v[i]=scan(x,i,',');
   end;
   drop i;
run;&lt;/PRE&gt;
&lt;P&gt;The array is to create a number of variables to store each value in. In this case I have picked an arbitrary number of 15 variables, which would be named v1, v1, ... v15. Each can hold a maximum of 50 characters, the $ 50. If you need more or fewer variables adjust the number in parentheses on the Array statement. If the values need to be longer increase the 50 or reduce.&lt;/P&gt;
&lt;P&gt;The Countw function in this use only counts commas as the delimiter be between "words", so we can use that to extract each "word" using the SCAN function, which again has been told to only use the comma as delimiter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jul 2021 17:08:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-text-between-commas/m-p/756718#M238974</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-07-26T17:08:47Z</dc:date>
    </item>
    <item>
      <title>Re: extracting text between commas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-text-between-commas/m-p/757499#M239119</link>
      <description>&lt;P&gt;Thank you, Ballardw.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As I've studied the idiosyncrasies of my data, I'd love to get your advice. Basically I have a column of data that includes a person's career history. The jobs are separated by semicolons. Generally, all the jobs have Titles. Some of the Jobs have an Office (or multiple offices) and an Organization (all have an organization). Some have the years of service, contained in parenthesis, some don't have the dates of service. Below is an example of the text that is in the column that I'm trying to distill.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Example Text:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;"United States Army Officer, Office of Strategy, United States Department of the Army (1980-2009); Highly Qualified Expert, United States Department of the Army (2009-2011); Director, U.S. Army Heritage and Education Center, United States Department of the Army, United States Department of Defense; Assistant Chief, Museum Support Center, U.S. Army Center of Military History, United States Department of Defense; Member, Department of Defense Historical Advisory Committee, Office of the Secretary of Defense, United States Department of Defense (2011-2012); Director and Chief of Military History, U.S. Army Center of Military History, Office of the Administrative Assistant to the Secretary, United States Department of the Army (2011-2014); Chair, The Commission, World War I Centennial Commission; Acting Secretary, American Battle Monuments Commission (2017-2018)"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Ultimately, I'd like to end up with as close an approximation as possible to the following:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Title: United States Army Officer&lt;/P&gt;&lt;P&gt;Office 1: Office of Strategy&lt;/P&gt;&lt;P&gt;Organization: United States Department of the Army&lt;/P&gt;&lt;P&gt;Start Year: 1980&lt;/P&gt;&lt;P&gt;End Year: 2009&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Title: Highly Qualified Expert&lt;/P&gt;&lt;P&gt;Organization: United States Department of the Army&lt;/P&gt;&lt;P&gt;Start Year: 2009&lt;/P&gt;&lt;P&gt;End Year: 2011&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Title: Director&lt;/P&gt;&lt;P&gt;Office1: U.S. Army Heritage and Education Center&lt;/P&gt;&lt;P&gt;Office2: United States Department of the Army&lt;/P&gt;&lt;P&gt;Organization: United States Department of Defense&lt;/P&gt;&lt;P&gt;Start Year:&lt;/P&gt;&lt;P&gt;End Year:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I appreciate your guidance!&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jul 2021 19:43:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-text-between-commas/m-p/757499#M239119</guid>
      <dc:creator>PavSingh</dc:creator>
      <dc:date>2021-07-27T19:43:27Z</dc:date>
    </item>
    <item>
      <title>Re: extracting text between commas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-text-between-commas/m-p/757510#M239121</link>
      <description>&lt;P&gt;If there is ALWAYS a semicolon between the "jobs" then it is the same thing basically. You just need another loop outside of the example that will select the jobs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But a working example of code would require showing what the resulting DATA SET looks like.&lt;/P&gt;
&lt;P&gt;I might be tempted to make one observation per JOB for clarity.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data example;
   x="United States Army Officer, Office of Strategy, United States Department of the Army (1980-2009); Highly Qualified Expert, United States Department of the Army (2009-2011); Director, U.S. Army Heritage and Education Center, United States Department of the Army, United States Department of Defense; Assistant Chief, Museum Support Center, U.S. Army Center of Military History, United States Department of Defense; Member, Department of Defense Historical Advisory Committee, Office of the Secretary of Defense, United States Department of Defense (2011-2012); Director and Chief of Military History, U.S. Army Center of Military History, Office of the Administrative Assistant to the Secretary, United States Department of the Army (2011-2014); Chair, The Commission, World War I Centennial Commission; Acting Secretary, American Battle Monuments Commission (2017-2018)";
   Array v (15) $ 50;
   length jobtext $ 200;
   do jobnum=1 to countw(x,';');
      jobtext = scan(x,jobnum,';');
      do i=1 to countw(jobtext,',');
         v[i]=scan(jobtext,i,',');
      end;
      output;
   end;
   drop i;
run;&lt;/PRE&gt;
&lt;P&gt;What we cannot do by code because each "job" does not have the the same fields is tell what the ones between "title" and "organization" may be because there are differing numbers of values based on your rules of "separate by comma".&lt;/P&gt;
&lt;P&gt;You can try to parse out years from the last "word" in the JOBTEXT (i.e. when i=countw(jobtext,','); IF the data is regular enough it just another application of SCAN with ( - and ) used as the delimiters (no "countw" needed for that). However if there are ever any other uses of ( - or ) character that may not work.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jul 2021 19:57:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-text-between-commas/m-p/757510#M239121</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-07-27T19:57:21Z</dc:date>
    </item>
  </channel>
</rss>

