DATA Step, Macro, Functions and more

Parse text string for "Wyoming" or "WY" from txt file

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Parse text string for "Wyoming" or "WY" from txt file

Hello I would like to get all the references from a text file in Var2 that match Wyoming or WY.  I did right a routine that grabs WY specific items in VAR1 that substr(var1, 5, 7) = "WY";

This will grab most but not all of the files are consistent. So I would like to match things from the text field with the words wyoming, wy and etc.  See example.  And no Wyoming does not have a consistent place or structure in the text files. Sometimes it is middle and sometimes at the end of Var2. 

Var1Var2Var3VAR4Var5Last Updated
A\U\AUD10MD156N.csv10-Month London Interbank Offered Rate (LIBOR), based on Australian Dollar%DNSA2013-05-15
G\B\GBP10MD156N.csv10-Month London Interbank Offered Rate (LIBOR), based on British Pound%DNSA2013-05-15
C\A\CAD10MD156N.csv10-Month London Interbank Offered Rate (LIBOR), based on Canadian Dollar%DNSA2013-05-15
D\K\DKK10MD156N.csv10-Month London Interbank Offered Rate (LIBOR), based on Danish Krone (DISCONTINUED SERIES)%DNSA2013-04-10
E\U\EUR10MD156N.csv10-Month London Interbank Offered Rate (LIBOR), based on Euro%DNSA2013-05-15
J\P\JPY10MD156N.csv10-Month London Interbank Offered Rate (LIBOR), based on Japanese Yen%DNSA2013-05-15
N\Z\NZD10MD156N.csv10-Month London Interbank Offered Rate (LIBOR), based on New Zealand Dollar (DISCONTINUED SERIES)%DNSA2013-03-07
S\E\SEK10MD156N.csv10-Month London Interbank Offered Rate (LIBOR), based on Swedish Krona (DISCONTINUED SERIES)%DNSA2013-04-10
C\H\CHF10MD156N.csv10-Month London Interbank Offered Rate (LIBOR), based on Swiss Frank%DNSA2013-05-15
U\USD10MD156N.csv10-Month London Interbank Offered Rate (LIBOR), based on U.S. Dollar%DNSA2013-05-15
D\T\DTP10J22.csv10-Year 0-1/8% Treasury Inflation-Indexed Note, Due 1/15/2022%DNSA2013-05-15
D\T\DTP10J23.csv10-Year 0-1/8% Treasury Inflation-Indexed Note, Due 1/15/2023%DNSA2013-05-15
D\T\DTP10L22.csv10-Year 0-1/8% Treasury Inflation-Indexed Note, Due 7/15/2022%DNSA2013-05-15
D\T\DTP10L21.csv10-Year 0-5/8% Treasury Inflation-Indexed Note, Due 7/15/2021%DNSA2013-05-15
D\T\DTP10L20.csv10-Year 1-1/4% Treasury Inflation-Indexed Note, Due 7/15/2020%DNSA2013-05-15
T\P\TP10L20.csv10-Year 1-1/4% Treasury Inflation-Indexed Note, Due 7/15/2020%MNSA2013-05-01

Accepted Solutions
Solution
‎06-06-2013 06:03 PM
Respected Advisor
Posts: 4,659

Re: Parse text string for "Wyoming" or "WY" from txt file

It is simple to do with Perl regular expression matching :

data wy;

set test;

if prxmatch("/\b(wy|wyoming)\b/io", var2);

run;

With your example data, it matches nothing.

PG

PG

View solution in original post


All Replies
Solution
‎06-06-2013 06:03 PM
Respected Advisor
Posts: 4,659

Re: Parse text string for "Wyoming" or "WY" from txt file

It is simple to do with Perl regular expression matching :

data wy;

set test;

if prxmatch("/\b(wy|wyoming)\b/io", var2);

run;

With your example data, it matches nothing.

PG

PG
Respected Advisor
Posts: 3,124

Re: Parse text string for "Wyoming" or "WY" from txt file

PG, "/io" part of your code, I guess 'i' is to ignore case, is it? and what the 'o' do?

Thanks,

Haikuo

Respected Advisor
Posts: 4,659

Re: Parse text string for "Wyoming" or "WY" from txt file

Right. i requires to ignore the case, o requests that the pattern be compiled only once during the datastep execution (otherwise it is recompiled at every call). - PG

PG
Respected Advisor
Posts: 3,124

Re: Parse text string for "Wyoming" or "WY" from txt file

I see. I have been always wondering and always forgot asking. Thank you, PG!

Super User
Posts: 10,538

Re: Parse text string for "Wyoming" or "WY" from txt file

It may be that the function INDEXW is your friend here.

You didn't say how you want to indicate the presence but

data want;

     set have;

     WYflag = (indexw(upcase(var2),'WYOMING') >0 or indexw(upcaase(var2),'WY')>0);

run;

should create a flag variable =1 when either of the strings occur as a word ( for example showy wouldn't be counted as a match for wy) and 0 when neither is present in VAR2.

You may want to add delimiters to the INDEXW function if you data is likely to have something besides a space separating the words of interest.

Occasional Contributor
Posts: 9

Re: Parse text string for "Wyoming" or "WY" from txt file

That works, I've not messed with prxmatch thanks,

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 214 views
  • 0 likes
  • 4 in conversation