1
votes

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
What if such a character occurs in the middle of the line? - Dirk Horsten
No i just want to remove from beginning and end. - Aditi
Could you post an example dataset? Natural Expression is a way forward here. - Vasilij Nevlev
%*- Step 05: Data manipulation steps to prepare for reporting and -; %- output results -*; So all i want is, to strip out the characters from extremities to get the text within these comments. - Aditi
What is your definition of beginning and end? Looks like the number of special characters at the beginning of a line can vary. So you want to replace all of the special chars until you get to the first non-special char? And same for the end? - Quentin

3 Answers

2
votes
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;
0
votes

please, take a look at translate function in sas. the first argument is your variable, the second argument is blank (the term you will have), third argument is a list of all your special chars that need to be replaced with second parameter.

translate(variable,' ','*%-');

0
votes

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.