I made a dataset in SAS that reads a text file line by line. So while I read those lines in my dataset, i want to eliminate special characters like *,%,-,; from the beginning and end of that particular line. what function should i use? The characters may occur in any sequence and i have to replace them by space. Please help!
3 Answers
data forAditi;
infile datalines truncover;
format aLine translated parced $80.;
input @1 aLine $char80.;
** The old school translate function does a good job but also translates characters in the middle **;
translated = translate(aLine,' ','* % - ;');
** Therefore you might prefer regular expressions **;
retain prx_nr;
if _N_ EQ 1 then prx_nr = prxparse('/[ *%-;]*(.+[^ *%-;])/') ;
match = prxmatch(prx_nr, aLine);
call prxposn(prx_nr, 1, pos, len);
substr(parced,pos) = prxposn(prx_nr, 1, aLine);
** [ *%-;]* looks for zero or more special characters, .+ looks for 1 or more characters what so ever and [^ *%-;] looks for any non special character. prxmatch will look for the longest possible match, so starting at the first character, special or not and ending at the last non-special character. prxposn, however, will set the position and length to the part of the match enclosed in (...), i.e. from the first non special character till the last. Now using the fact that SAS reinitializes all its variables unless explicitly retained, we just have to copy that part at the right position into parced **;
datalines4;
This is text;
--That should be cleaned up,
And here- you have *% special characters in the middle.
Blanks at the start should be preserved. Right?
;;;;
run;
You can use the compress function to remove special characters, either using a defined list of characters, or the 'p' option (remove all punctuation/special chars). To ensure they're only removed at the start/end, also use substr :
/* Assuming 'text' is always 3 or more characters */ data want ; set have ; strStart = substr(text,1,1) ; strEnd = substr(text,length(text),1) ; strMid = substr(text,2,length(text)-2) ; newStart = compress(strStart,,'p') ; /* remove all non-alphanumeric */ newEnd = compress(strEnd ,,'p') ; newStr = cats(newStart,strMid,newEnd) ; run ;
You could consolidate all those operations into a single statement.