X=”GTTCACTAGCAACCTCAAACAGACACCATGGTGCACCTG”
a=”-CCTCA”
b=”A-CTCA”
c=”AC-TCA”
d=”ACC-CA”
e=”ACCT-A”
f=”ACCTC-”
“-“ is any one letter, such as “G” “T” or “A”;
I thought a,b,c,d,e,f are all equal to pattern “ACCTCA”
How can I write an index function to find the position of a (or b, or c, or d, or e, or f) in X;
Thanks.
You say "pattern" I think RegEx. Below a variation of what has already been proposed.
data sample;
input string:$60.;
match_pos= prxmatch('/(.CCTCA|A.CTCA|AC.TCA|ACC.CA|ACCT.A|ACCTC.)/oi',string);
match_flg= prxmatch('/(.CCTCA|A.CTCA|AC.TCA|ACC.CA|ACCT.A|ACCTC.)/oi',string)>0;
datalines;
GTTCACTAGCAACCTCAAACAGACACCATGGTGCACCTG
GTTCACTAGCACACTCAAACAGACACCATGGTGCACCTG
;
run;
I'm no great Perl maven, but using prxmatch will give you what you're after. Here's how I did it:
data _null_; infile cards; retain x "GTTCACTAGCAACCTCAAACAGACACCATGGTGCACCTG"; length pattern $ 9; input pattern; match = prxmatch('/' || strip(pattern) || '/', x); put match=; cards; .CCTCA A.CTCA AC.TCA ACC.CA ACCT.A ACCTC. ; run;
In Perl, the wild card is '.', not '-'.
When this code is run, the pattern is found at position 12 every time. If it weren't found, like with index, it would return 0.
Thanks a lot. I never used prxmatch function. I will try it.
find() or index() don't allow for wildcard searches.
You could either use a Regular Expression or a Like operator in a where clause.
Would you just need the first position where one of your patterns matches? And would ALL patterns have to match or only one of them?
Also, look into COMPGED, Complev and other functions.
Not sure they'll give you enough control though.
X=”GTTCACTAGCAACCTCAAACAGACACCATGGTGCACCTG”
pattern="ACCTCA" or pattern="A-CTCA"
Actually I want to check " if X contains pattern ";
I found I can not use this logistic judgement,
So I thought maybe I can use index() function,
if result >0 it means X contains pattern;
if result =0 it means X does not contains pattern.
if someone can suggest a better method, it will be great.
You say "pattern" I think RegEx. Below a variation of what has already been proposed.
data sample;
input string:$60.;
match_pos= prxmatch('/(.CCTCA|A.CTCA|AC.TCA|ACC.CA|ACCT.A|ACCTC.)/oi',string);
match_flg= prxmatch('/(.CCTCA|A.CTCA|AC.TCA|ACC.CA|ACCT.A|ACCTC.)/oi',string)>0;
datalines;
GTTCACTAGCAACCTCAAACAGACACCATGGTGCACCTG
GTTCACTAGCACACTCAAACAGACACCATGGTGCACCTG
;
run;
Thanks a lot. It is very helpful.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.