BookmarkSubscribeRSS Feed
YW_CA
Calcite | Level 5


hi, any ideas how to modify a data set of pure text?

I have a text file which need to be modified, it was imported all in one single column. The file has different section, each section end with end of xxx section data.  An example of the file look like below

a,b,c,d

end of ppp section data

1,1,a,a,c,c

2,2,w,w,f,g

end of qqq section data

q,w,1,3,4,a,f,1

w,p,1,4,5,j,n,2

end of www section data

I need to add some data when I see end of. for example, when there is "end of ppp section data", I want to add k,l,m,n. When there is "end of qqq section data", I want to add 9,9,q,s,d,v.

Any ideas?

Thank you.

4 REPLIES 4
PGStats
Opal | Level 21

The most robust and flexible approach is probably PRX string matching :

data test;
length line $100;
input;
line = _infile_;
datalines;
a,b,c,d
end of ppp section data
1,1,a,a,c,c
2,2,w,w,f,g
end of qqq section data
q,w,1,3,4,a,f,1
w,p,1,4,5,j,n,2
end of www section data
;

data want(drop=tmpLine what add PRXid);
retain PRXid;
length tmpLine $100 what $32 add $64;
if _n_=1 then PRXid = prxparse("/^\s*end\s+of\s+(\w+)\s+section\s+data/i");
set test;
if prxmatch(PRXid, line) then do;
     what = prxposn(PRXid, 1, line);
     select (upcase(what));         

          when ("PPP") add = "k,l,m,n" ;
          when ("QQQ") add = "9,9,q,s,d,v";
          otherwise add = "UNKNOWN SECTION";
          end;
     tmpLine = line;
     line = add;
     output;
     line = tmpLine;
     end;
output;
run;

PG

PG
JuliaM
Calcite | Level 5

Does PRX refer to Perl Regular Expressions?

If so, more information can be found at: SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

If not, PGStats, can you tell us what PRX means?

PGStats
Opal | Level 21

Yes, PRX is the prefix for SAS functions implementing Perl Regular Expressions. As displayed in my code, prxparse compiles a regular expression, prxmatch finds matches and prxposn extracts a substring from a match. - PG

PG
YW_CA
Calcite | Level 5

This works for me. Thank you.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to choose a machine learning algorithm

Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1619 views
  • 0 likes
  • 3 in conversation