Date: Sat, 12 May 2012 20:56:58 -0400
Reply-To: Ken Borowiak <evilpettingzoo97@AOL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Ken Borowiak <evilpettingzoo97@AOL.COM>
Subject: Re: Perl match question. Using prxmatch to complete an if
contains statement.
On Sat, 12 May 2012 09:16:45 -0400, Peter Timusk
<petertimusk@PROGRAMDOCUMENTATION.COM> wrote:
>Hello all
>
>I was this morning hoping to do this
>
>If x_char contains 'y' then var =1;
>
>
>Apparently I can not use "contains" in an if statement.
>
>So I use this
>
>If substr(x_char, prxmatch(x_char,'y'), 1) = 'y' then var=1;
>
>My question is what does the prxmatch function return for a position
number, if it does not find the string you asked it to find? In other words
will my if statement cough up too many errors when observations of x_char
do not contain a 'y' character?
>
>Peter Timusk
Peter,
PRXMATCH returns the starting position in the string where it finds a
match. If no match is found it returns a value of 0.
To create a 0/1 indicator field you van use a Boolean on the right-hand
side of the assignment statement:
data _null_ ;
set sashelp.class ;
has_y1=( prxmatch('/y/io',name)>0 ) ;
has_y2=^^prxmatch('/y/io',name)>0 ;
put (name has_y1 has_y2)(=) ;
run ;
SAS log:
Name=Alfred has_y1=0 has_y2=0
Name=Alice has_y1=0 has_y2=0
Name=Barbara has_y1=0 has_y2=0
Name=Carol has_y1=0 has_y2=0
Name=Henry has_y1=1 has_y2=1
Name=James has_y1=0 has_y2=0
Name=Jane has_y1=0 has_y2=0
Name=Janet has_y1=0 has_y2=0
Name=Jeffrey has_y1=1 has_y2=1
Name=John has_y1=0 has_y2=0
Name=Joyce has_y1=1 has_y2=1
Name=Judy has_y1=1 has_y2=1
Name=Louise has_y1=0 has_y2=0
Name=Mary has_y1=1 has_y2=1
Name=Philip has_y1=0 has_y2=0
Name=Robert has_y1=0 has_y2=0
Name=Ronald has_y1=0 has_y2=0
Name=Thomas has_y1=0 has_y2=0
Name=William has_y1=0 has_y2=0
Regards,
Ken Borowiak