0

I want to read a file and find the word “the” and introduce a line feed. i.e. find and replace the text ‘the’ to ‘/nthe’ Can you please help?

    /*input.txt*/
    Many a slip between the cup and the lip. 

    /*Required output*/
    Many a slip between 
    the cup and 
    the lip. 

    /*sas datastep*/
    data inp;
    infile "c:/tmp/input.txt";
    /*ADD LOGIC*/
    infile "c:/tmp/output.txt";
    run;
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
AKS
  • 184
  • 2
  • 18
  • several options, one would be http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000215027.htm – kl78 Jun 17 '16 at 09:07
  • Transwd is creating the text as '/n' instead of creating a line feed. test_txt = tranwrd(text, "the", "/nthe"); – AKS Jun 17 '16 at 10:04
  • 1
    linefeed is interpreted as '0A'x in sas, so try test_txt =tranwrd(text,"the",cat('0A'x,"the")); (cr is '0d'x if you also want to add it) – kl78 Jun 17 '16 at 10:14
  • Works fine, Thanks @kl78. – AKS Jun 17 '16 at 10:41
  • @kl78 Please enter those into an answer so the OP can accept it and effectively close this question. – Robert Penridge Jun 19 '16 at 15:58

1 Answers1

4

Already answered in comments, summarized as answer

There are several options to do a find and replace in SAS, I would suggest using tranwrd.

SAS interprets a linefeed as '0A'x.
For a carriage return you would use '0D'x.

So the solution for you would be :

 test_txt =tranwrd(text,"the",cat('0A'x,"the")); 
kl78
  • 1,628
  • 1
  • 16
  • 26