DATA Step, Macro, Functions and more

how to use Regular expression read strings on multiple lines

Reply
Regular Contributor
Posts: 152

how to use Regular expression read strings on multiple lines

Hello experts,

Can someone tell me why the metacharacter \n is not working for the program belw


data a;
infile cards truncover;
length a $1;
input a $150.;
cards;
a
b
run;

data b;
set a;

e=prxmatch("/a\nb",a);
run;

Thanks

Super User
Posts: 10,044

Re: how to use Regular expression read strings on multiple lines

You have two obs not one , therefore PRX can't work on that situation .

Regular Contributor
Posts: 152

Re: how to use Regular expression read strings on multiple lines

HI ksharp,

can an you use \n to read new line in regular expression?

Super Contributor
Posts: 308

Re: how to use Regular expression read strings on multiple lines

Hello,

Maybe this is what you are looking for:

data a;
a="103 Pennsylvania Ave. NW,";output;
a="Washington, DC 20216" ;output;
a="NY, DC 20216" ;output;
run;


data b;
set a;
b=prxparse("/103 Pennsylvania Ave\. NW|Washington, DC 20216/");
e= prxmatch(b,a);

run;

Check also the following link:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002295977.htm

Regular Contributor
Posts: 152

Re: how to use Regular expression read strings on multiple lines

I have modified the program to make it more simple.

Super User
Super User
Posts: 7,074

Re: how to use Regular expression read strings on multiple lines

No.

If you have organized your dataset so that each line is on a separate observation you cannot use the SAS perl regular expression functions to operate across observations.

To check if you ever have the condition that "B" follows "A" in your data set perhaps you want to use LAG() function?

Here is a trivial example .

data want ;

  input a $ ;

if a='B' and lag(a)='A' then found=1;

else found=0;

CARDS;

A

B

C

D

B

A

C

A

B

run;

In this case only the second and the final observation will have FOUND set to 1.

Contributor
Posts: 33

Re: how to use Regular expression read strings on multiple lines

I would not use SAS for this type of parsing of a structured text file. Use awk: it is designed for this. Awk is a small program available on almost all operating systems. It is part of unix. If you do not have awk, use perl, which is bigger and more complex. After you have used awk to reorganise the data, read it into SAS. You should be able to run the awk from SAS, or use a command script to run both. I use this method frequently.

Ask a Question
Discussion stats
  • 6 replies
  • 289 views
  • 0 likes
  • 5 in conversation