<?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 extract each value before a key word in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/extract-each-value-before-a-key-word/m-p/727166#M28181</link>
    <description>&lt;P&gt;Hello, I have the following:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input original_string $1-35;
    datalines;
I was 12 yearsold I am 50 yearsold
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to achieve the following:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    input desired_age1 desired_age2;
    datalines;
12 50
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I figured I need to use "SCAN" like below, except I need to figure out how to loop it so that I can capture multiple ages.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
do i=1 to countw(original_string,"");
	if scan(original_string,i,"")="yearsold" then do;
	  desired_age1=scan(original_string,i-1,"");
	end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;Any thoughts? I appreciate any help.&lt;/P&gt;</description>
    <pubDate>Wed, 17 Mar 2021 16:33:12 GMT</pubDate>
    <dc:creator>LFern</dc:creator>
    <dc:date>2021-03-17T16:33:12Z</dc:date>
    <item>
      <title>extract each value before a key word</title>
      <link>https://communities.sas.com/t5/New-SAS-User/extract-each-value-before-a-key-word/m-p/727166#M28181</link>
      <description>&lt;P&gt;Hello, I have the following:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input original_string $1-35;
    datalines;
I was 12 yearsold I am 50 yearsold
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to achieve the following:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    input desired_age1 desired_age2;
    datalines;
12 50
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I figured I need to use "SCAN" like below, except I need to figure out how to loop it so that I can capture multiple ages.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
do i=1 to countw(original_string,"");
	if scan(original_string,i,"")="yearsold" then do;
	  desired_age1=scan(original_string,i-1,"");
	end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;Any thoughts? I appreciate any help.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 16:33:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/extract-each-value-before-a-key-word/m-p/727166#M28181</guid>
      <dc:creator>LFern</dc:creator>
      <dc:date>2021-03-17T16:33:12Z</dc:date>
    </item>
    <item>
      <title>Re: extract each value before a key word</title>
      <link>https://communities.sas.com/t5/New-SAS-User/extract-each-value-before-a-key-word/m-p/727169#M28182</link>
      <description>&lt;P&gt;For the sample you have posted your code should work. Have you tried it?&lt;/P&gt;
&lt;P&gt;To get a better solution we need to see more test data and possible combinations.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 16:41:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/extract-each-value-before-a-key-word/m-p/727169#M28182</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-03-17T16:41:53Z</dc:date>
    </item>
    <item>
      <title>Re: extract each value before a key word</title>
      <link>https://communities.sas.com/t5/New-SAS-User/extract-each-value-before-a-key-word/m-p/727196#M28184</link>
      <description>&lt;P&gt;Hi Shmuel,&lt;/P&gt;&lt;P&gt;My posted code only yields "Desired_age1=50".&lt;/P&gt;&lt;P&gt;But I also need a variable called "Desired_age2=12" and I dont know how to get that second age.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 17:25:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/extract-each-value-before-a-key-word/m-p/727196#M28184</guid>
      <dc:creator>LFern</dc:creator>
      <dc:date>2021-03-17T17:25:15Z</dc:date>
    </item>
    <item>
      <title>Re: extract each value before a key word</title>
      <link>https://communities.sas.com/t5/New-SAS-User/extract-each-value-before-a-key-word/m-p/727211#M28186</link>
      <description>&lt;P&gt;You are right. I have fixed your code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
    array age {*} desired_age1 - desired_age2;
	do i=1 to countw(original_string,"");
		if scan(original_string,i,"")="yearsold" then do;
		  if i  le dim(age) then
		     age(i)=scan(original_string,i-1,"");
		end;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Mar 2021 18:16:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/extract-each-value-before-a-key-word/m-p/727211#M28186</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-03-17T18:16:14Z</dc:date>
    </item>
    <item>
      <title>Re: extract each value before a key word</title>
      <link>https://communities.sas.com/t5/New-SAS-User/extract-each-value-before-a-key-word/m-p/727346#M28191</link>
      <description>&lt;P&gt;Hello again,&lt;/P&gt;&lt;P&gt;Sorry to bother. The code you provided displays missing values.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SAS_want.PNG" style="width: 578px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/56097i54CE9906AF17C741/image-dimensions/578x352?v=v2" width="578" height="352" role="button" title="SAS_want.PNG" alt="SAS_want.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I'm not familiar with arrays but I did some quick research and I think the reason your code isn't working is because "Desired_Age1" &amp;amp; "Desired_Age2" are not variables in the "HAVE" dataset.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Mar 2021 10:01:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/extract-each-value-before-a-key-word/m-p/727346#M28191</guid>
      <dc:creator>LFern</dc:creator>
      <dc:date>2021-03-18T10:01:00Z</dc:date>
    </item>
    <item>
      <title>Re: extract each value before a key word</title>
      <link>https://communities.sas.com/t5/New-SAS-User/extract-each-value-before-a-key-word/m-p/727349#M28192</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/262848"&gt;@LFern&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does the following do what you require?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* set up input data */
data have;
   infile datalines truncover;
   input original_string $1-100;
   datalines;
I was 12 yearsold I am 50 yearsold
I was 22 yearsold
I am 33 yearsold
I was 44 yearsold I am 55 yearsold tomorrow I'm 56 yearsold
;


/* output each age to a new observation */
data want1;
   set have;

   do i = 1 to countw(original_string,"");
   	if scan(original_string,i,"") eq "yearsold" then
      do;
         desired_age = scan(original_string,i-1,"");
         output;
   	end;
   end;
run;


/* transpose desired_age by original_string */
proc transpose data   = want1
               out    = want2
               name   = original_string
               prefix = desired_age
               ;
   by original_string notsorted;
   var desired_age;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Mar 2021 10:17:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/extract-each-value-before-a-key-word/m-p/727349#M28192</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2021-03-18T10:17:10Z</dc:date>
    </item>
    <item>
      <title>Re: extract each value before a key word</title>
      <link>https://communities.sas.com/t5/New-SAS-User/extract-each-value-before-a-key-word/m-p/727361#M28193</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/262848"&gt;@LFern&lt;/a&gt;&amp;nbsp;, you are right. I fixed the code and tested it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input original_string $1-35;
    datalines;
I was 12 yearsold I am 50 yearsold
;
run;
data want;
set have;
    array age {*} desired_age1 - desired_age2;
    j=0;
	do i=1 to countw(original_string);
		if scan(original_string,i)="yearsold" then do;
		  j+1;
		  if j le dim(age) then 
		     age(j)=input(scan(original_string,i-1),best2.); 
		end;
	end;
	drop i j;
run;&lt;BR /&gt;/* space is a default delimiter of scan() function */&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Mar 2021 11:09:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/extract-each-value-before-a-key-word/m-p/727361#M28193</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-03-18T11:09:01Z</dc:date>
    </item>
    <item>
      <title>Re: extract each value before a key word</title>
      <link>https://communities.sas.com/t5/New-SAS-User/extract-each-value-before-a-key-word/m-p/727367#M28195</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   infile datalines truncover;
   input original_string $1-100;
   datalines;
I was 12 yearsold I am 50 yearsold
I was 22 yearsold
I am 33 yearsold
I was 44 yearsold I am 55 yearsold tomorrow I'm 56 yearsold
;

data temp;
 set have;
 id+1;
 pid=prxparse('/\d+\s*(?=yearsold)/i');
 s=1;e=length(original_string);
 call prxnext(pid,s,e,original_string,p,l);
 do while(p&amp;gt;0);
  want=substr(original_string,p,l);output;
  call prxnext(pid,s,e,original_string,p,l);
 end;
 drop s e p l pid;
run;
proc transpose data=temp out=want(drop=_:);
by id original_string;
var want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Mar 2021 11:45:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/extract-each-value-before-a-key-word/m-p/727367#M28195</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-03-18T11:45:45Z</dc:date>
    </item>
    <item>
      <title>Re: extract each value before a key word</title>
      <link>https://communities.sas.com/t5/New-SAS-User/extract-each-value-before-a-key-word/m-p/727450#M28199</link>
      <description>&lt;P&gt;This was fantastic, thank you so much!&lt;/P&gt;</description>
      <pubDate>Thu, 18 Mar 2021 15:42:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/extract-each-value-before-a-key-word/m-p/727450#M28199</guid>
      <dc:creator>LFern</dc:creator>
      <dc:date>2021-03-18T15:42:06Z</dc:date>
    </item>
  </channel>
</rss>

