<?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 extract a substring and generate a new variable with values of that substring in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-substring-and-generate-a-new-variable-with/m-p/889421#M351406</link>
    <description>&lt;P&gt;What result would you like for this input?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Exercise twice per day for 14 days, then reduce to once per day for 5 days.&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 15 Aug 2023 19:06:05 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2023-08-15T19:06:05Z</dc:date>
    <item>
      <title>How to extract a substring and generate a new variable with values of that substring</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-substring-and-generate-a-new-variable-with/m-p/889418#M351403</link>
      <description>&lt;P&gt;Hi SAS Pro,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm having a question that I want to extract "14 days" or "10 days" and any "digit days" texts from sentences. The tricky thing is that the position of the substring could be various.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;/P&gt;
&lt;P&gt;input X $80.;&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;I spent 14 days in Boston. I love it.&lt;/P&gt;
&lt;P&gt;Exercise twice a day for 10 days until May.&lt;/P&gt;
&lt;P&gt;I left there. It was about 7 days ago.&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I want is to have&lt;/P&gt;
&lt;P&gt;1. a new character variable Y including 14 days, 10 days, and 7 days.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. a numeric variable Z with the values of 14, 10, and 7.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help would be highly appreciated!!&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;C&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 18:59:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-substring-and-generate-a-new-variable-with/m-p/889418#M351403</guid>
      <dc:creator>CynthiaWei</dc:creator>
      <dc:date>2023-08-15T18:59:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a substring and generate a new variable with values of that substring</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-substring-and-generate-a-new-variable-with/m-p/889421#M351406</link>
      <description>&lt;P&gt;What result would you like for this input?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Exercise twice per day for 14 days, then reduce to once per day for 5 days.&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Aug 2023 19:06:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-substring-and-generate-a-new-variable-with/m-p/889421#M351406</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-08-15T19:06:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a substring and generate a new variable with values of that substring</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-substring-and-generate-a-new-variable-with/m-p/889424#M351408</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/266374"&gt;@CynthiaWei&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS PRX functions are great for this kind of work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input X $80.;
datalines;
I spent 14 days in Boston. I love it.
Exercise twice a day for 10 days until May.
I left there. It was about 7 days ago.
;
run;

data want;
  set have;
  length Y $8;
  y = prxchange('s/(.*)(\s\d+\s\D+\s) (.*)/$2/',-1,X);
  Z = input(scan(Y,1,' '),8.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="extractdays.gif" style="width: 469px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/86735iAEB6B57ED52AFA56/image-size/large?v=v2&amp;amp;px=999" role="button" title="extractdays.gif" alt="extractdays.gif" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 19:14:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-substring-and-generate-a-new-variable-with/m-p/889424#M351408</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2023-08-15T19:14:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a substring and generate a new variable with values of that substring</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-substring-and-generate-a-new-variable-with/m-p/889425#M351409</link>
      <description>&lt;P&gt;You want to find the word "days" somewhere in this text string, and then extract the previous "word" which is probably going to be the number you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    do i=1 to countw(x,' ');
        if scan(x,i,' ')='days' then do;
           number_of_days=input(scan(x,i-1,' '),4.); /* Input turns this number of days into a numeric value */
           output;
        end;
    end;
    drop i;
run;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 19:21:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-substring-and-generate-a-new-variable-with/m-p/889425#M351409</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-08-15T19:21:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a substring and generate a new variable with values of that substring</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-substring-and-generate-a-new-variable-with/m-p/889426#M351410</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/266374"&gt;@CynthiaWei&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Updated solution&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My previous attempt worked with your data, this is more robust:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input X $80.;
datalines;
I spent 14 days in Boston. I love it.
Exercise twice a day for 10 days until May.
I left there. It was about 7 days ago.
I had to wait 7 days
8 days is more than I care to wait
I have waited 12 days 3 times
;
run;

data want;
  set have;
  length Y $8;
  y = prxchange('s/(.*\s*)(\d+\s)(days\s)(.*)/$2$3/',-1,X);
  Z = input(scan(Y,1,' '),8.);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Aug 2023 19:30:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-substring-and-generate-a-new-variable-with/m-p/889426#M351410</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2023-08-15T19:30:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a substring and generate a new variable with values of that substring</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-substring-and-generate-a-new-variable-with/m-p/889427#M351411</link>
      <description>Thank you for asking this. What are the syntax if a. I only want the first text and b. any text that meets the target text.&lt;BR /&gt;&lt;BR /&gt;Thanks a lot!</description>
      <pubDate>Tue, 15 Aug 2023 19:46:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-substring-and-generate-a-new-variable-with/m-p/889427#M351411</guid>
      <dc:creator>CynthiaWei</dc:creator>
      <dc:date>2023-08-15T19:46:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract a substring and generate a new variable with values of that substring</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-substring-and-generate-a-new-variable-with/m-p/889430#M351413</link>
      <description>Thank you so much for the code. Just out of curiosity, what do those statements mean within the PRX function?</description>
      <pubDate>Tue, 15 Aug 2023 19:53:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-a-substring-and-generate-a-new-variable-with/m-p/889430#M351413</guid>
      <dc:creator>CynthiaWei</dc:creator>
      <dc:date>2023-08-15T19:53:24Z</dc:date>
    </item>
  </channel>
</rss>

