Help using Base SAS procedures

replace using infile and file.

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 80
Accepted Solution

replace using infile and file.

Hi people

Could anyone help me please?

I need a SAS code to replace the value <br> to \n from a txt file.

The problem is. This txt file is combined in just one single line thus its lrecl contain 223657.

Anybody can help me please?


I've attached the file. I need to change the values "<br>" to "\n." from txt file to the same txt file. That is replace it.


Please, if I could'n t be clear, let me know.

Thanks all.

Attachment

Accepted Solutions
Solution
‎01-20-2012 01:51 PM
PROC Star
Posts: 7,363

replace using infile and file.

You may be in luck.  I reviewed your file and, from what I could tell, every time there is a less than character (i.e., <), it is followed by 'br><br>' and then two spaces.

If that is correct, it simplifies matters quite a bit.

Another thing I noticed is that the file contains numerous tab characters (i.e., '09'x).  I wasn't sure what you wanted to do with them but, given the following code, they, too, could be easily eliminated.

What the code does is read the file one character at a time.  If it confronts a < character, it simply skips the next seven characters and, in their place, puts in a carriage return and line feed.

Then, after it rewrites the entire file, simply imports it with proc import:

data _NULL_ ;

  FILE "c:\pedacoMarco_new.txt" RECFM=N ;

  infile "c:\pedacoMarco.txt" RECFM=N ;

  input VAR1 $CHAR1. @;

  IF VAR1 eq '<' THEN do;

    do i=1 to 7;

    input VAR1 $CHAR1. @;

    end;

    put '0D'x;

    put '0A'x;

  end;

  else PUT VAR1 $CHAR1. ;

run;

PROC IMPORT OUT= WORK.WANT

            DATAFILE= "C:\pedacoMarco_new.txt"

            DBMS=DLM REPLACE;

     DELIMITER='7C'x;

     GETNAMES=YES;

     DATAROW=2;

RUN;

View solution in original post


All Replies
Solution
‎01-20-2012 01:51 PM
PROC Star
Posts: 7,363

replace using infile and file.

You may be in luck.  I reviewed your file and, from what I could tell, every time there is a less than character (i.e., <), it is followed by 'br><br>' and then two spaces.

If that is correct, it simplifies matters quite a bit.

Another thing I noticed is that the file contains numerous tab characters (i.e., '09'x).  I wasn't sure what you wanted to do with them but, given the following code, they, too, could be easily eliminated.

What the code does is read the file one character at a time.  If it confronts a < character, it simply skips the next seven characters and, in their place, puts in a carriage return and line feed.

Then, after it rewrites the entire file, simply imports it with proc import:

data _NULL_ ;

  FILE "c:\pedacoMarco_new.txt" RECFM=N ;

  infile "c:\pedacoMarco.txt" RECFM=N ;

  input VAR1 $CHAR1. @;

  IF VAR1 eq '<' THEN do;

    do i=1 to 7;

    input VAR1 $CHAR1. @;

    end;

    put '0D'x;

    put '0A'x;

  end;

  else PUT VAR1 $CHAR1. ;

run;

PROC IMPORT OUT= WORK.WANT

            DATAFILE= "C:\pedacoMarco_new.txt"

            DBMS=DLM REPLACE;

     DELIMITER='7C'x;

     GETNAMES=YES;

     DATAROW=2;

RUN;

Frequent Contributor
Posts: 80

replace using infile and file.

Sr Art.

You are definetely genius. Thanks so much it worked perfectly. I just used the String "\n" rather then  put '0D'x;  put '0A'x. Thanks again you always respond my discussion with a great solution.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 1105 views
  • 0 likes
  • 2 in conversation