sas >> How to replace CRLF with SPACE

by rune » Tue, 07 Oct 2003 22:44:04 GMT

I want to replace CRLF with SPACE in the file sak. I read the file sak,
and write the result back to a file I have given the name sak_ny
&rf_sd resolves to "F". Every record in the file sak starts with the letter "S".
I use the following code:

data _null_;
infile sak lrecl=280 recfm=&rf_sd;
file sak_ny lrecl=280 recfm=&rf_sd;
input;
if length(_infile_)<280 and substr(_infile_,1,1)='S' then
do;
_infile_=compress(_infile_,'0d'x!!'0a'x)!!" ";
put _infile_ @;
end;
run;


The problem is that I don't see the expected change in sak_ny. CRLF is
just removed and not replaced by SPACE.
What have I done wrong ?

Regards

Rune Runnestoe

sas >> How to replace CRLF with SPACE

by Howard_Schreier » Wed, 08 Oct 2003 00:49:23 GMT


The COMPRESS function removes characters. It does not do replacements. Your
assignment statement as coded removes all of the CR and LF characters and
concatenates two blanks at the end (not where the removals took place).

Consider the TRANWRD function instead. Also notice that you can code a
single hex constant for multiple bytes. So try something like:

_infile_=tranwrd(_infile_,'0d0a'x," ");



letter "S".

sas >> How to replace CRLF with SPACE

by pchoate » Wed, 08 Oct 2003 01:02:16 GMT

Can you explain "length(_infile_)<280" ? Unless byte 280 is blank the infile
should always be 280 if lrecl=280 and recfm=f. The idea behind recfm=f is
that the data is read as a single string, ignoring breaks, so the _infile_
length will always be the lrecl unless it has a blank in the final
position(s).

This seems to work, (notice that the file lrecl is ignored, but the trailing
@ is needed) - the first 20 bytes are read
0,1,2,3,4,5,6,7,8,9,CR,LF,01,2,3,4,5,6,7, the CR,LF is removed, ' ' is
appeded to the end, the line is output with the line-hold specifier, the
process is repeated...

8 data _null_;
9 infile 'C:\Docume~1\pchoate\Desktop\in.txt' lrecl=20 recfm=f;
10 file 'C:\Docume~1\pchoate\Desktop\out.txt' lrecl=20 recfm=f;
11 input;
12 _infile_=compress(_infile_,'0d'x!!'0a'x)!!" ";
13 put _infile_ @;
14 run;

NOTE: The infile 'C:\Docume~1\pchoate\Desktop\in.txt' is:
File Name=C:\Docume~1\pchoate\Desktop\in.txt,
RECFM=F,LRECL=20

NOTE: The file 'C:\Docume~1\pchoate\Desktop\out.txt' is:
File Name=C:\Docume~1\pchoate\Desktop\out.txt,
RECFM=F,LRECL=20

NOTE: 6 records were read from the infile
'C:\Docume~1\pchoate\Desktop\in.txt'.
NOTE: 6 records were written to the file
'C:\Docume~1\pchoate\Desktop\out.txt'.
NOTE: DATA statement used:
real time 0.09 seconds
cpu time 0.00 seconds

in.txt
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789

out.txt (all one line)
012345678901234567 8901234567890123 4567890123456789
012345678901234567 8901234567890123 4567890123456789

hth

Paul Choate
DDS Data Extraction
(916) 654-2160

-----Original Message-----
From: Rune Runnestoe [mailto: XXXX@XXXXX.COM ]
Sent: Tuesday, October 07, 2003 7:44 AM
To: XXXX@XXXXX.COM
Subject: How to replace CRLF with SPACE

I want to replace CRLF with SPACE in the file sak. I read the file sak,
and write the result back to a file I have given the name sak_ny
&rf_sd resolves to "F". Every record in the file sak starts with the letter
"S".
I use the following code:

data _null_;
infile sak lrecl=280 recfm=&rf_sd;
file sak_ny lrecl=280 recfm=&rf_sd;
input;
if length(_infile_)<280 and substr(_infile_,1,1)='S' then
do;
_infile_=compress(_infile_,'0d'x!!'0a'x)!!" ";
put _infile_ @;
end;
run;


The problem is that I don't see the expected change in sak_ny. CRLF is
just removed and not replaced by SPACE.
What have I done wrong ?

Regards

Rune Runnestoe

Similar Threads

1. replacing each occurence of crlf with space

2. any function to replace a space to underscore

I have value in character variable that contains spaces but I need replace
with underscore.
So for e.g. Variable Name has following values
"Compose a new message"
I need this to replace to
"Compose_a_new_message"

I tried to use Trnswrd or Translate but it places underscore at the end.

Does anyone knows function that I can replace spaces to underscore?

3. gsfmode=replace does not replace

4. Excel spreadsheet double space converted to single space

I am using ODS to output an html file with an ".xls" extension and
then later reading the .xls file using dde back into a sas dataset.
This is part of a QC process. What I am noticing is that where I have
"(  0.0)" (i.e. two spaces before "0.0" in brackets) then somewhere
during this process of writing out the html file or reading it back
into Excel and converting to a sas dataset then the double space gets
converted to a single space and I don't know why. Does anyone have any
ideas on this?

Also, why are people so set on converting the ascii collating
character 10 into null? I have seen this in a few places.

5. Need to remove a leading space without removing embedded spaces

6. problems when substituting crlf occuring inside records

Hi,

When moving data from mainframe mascines (IBM) to Windows PC's, it
often happens that CRLF is dropped in the middle of records. This make
problems why SAS interpret CRLF as the end of a record.


This is the file I have in 'D:\TEST\_TESTFILER\bad_file.txt'

02065170001001PSAKO20021223UDI 20030324AESN OPA2 VISION 24122002

00023090004002PVEDT20030430UDI 20030605AESROMEMHSAS  152  
20030430000000003 MND. SESONGARBEIDSTILL. FRA INNREISE
V NN                                                         
01038105002001PSAKO20030212UDI 20030305AESNFM/4 BARN

01006275003002PVEDT20030326UDI 20030605AESROMEMHSAS  152  
20030326000000003 MND. SESONGARBEIDSTILL. FRA INNREISE
V NN                                                         
02037265001001PSAKO20020719UDI 20020930AESN DUF SAK!

03004075001001PSAKO20030130UDI 20030509AESN
REG.SWE.020902.DOSSNR.9-760291.

03011765001002PVEDT20030325UDI 20030512AESUDI KATAB  154  
20030501200308013 MND FRA MELD. SISTEINNR 15.05.03
93036500002003PEKSP19931018UDI 19931018ILRASKEASKER OG BUM
POLITIKAMMER   J   KOPI ADV. JOHS. STRENGE NS
93036500002004PEKSP19931021ASKE19931021VIKASKEASKER OG BUM
POLITIKAMMER   JTH MOTTATT ASKE 211093
93036500002005PEFFE19931124ASKE19931124VIKASKEKNV
93036500002006PARKI19931124ASKE19931124VIKASKEASKER OG BUM
POLITIKAMMER
93036500002007PKORU19931124ASKE19931124VIKASKEASKER OG BUM
POLITIKAMMER   GUDBGUDBRANDSDAL POLITIKAMMER     BSN-PASS, KOPI AV
UDIS VEDTAK, ID-KORT,
02059900001001PSAKO20021112UDI 20030603AESN 191102 OPA4

01036500001022PKORU20011122UDI 20011123AESOSLOOSLO POLITIDISTRIKT     
     UDI UTLENDINGSDIREKTORATET        VEDLE
                                                           

To make this a good file, I have run the following code:
data _null_;
	infile 'D:\TEST\_TESTFILER\bad_file.txt' lrecl=175 recfm=V;
	file 'D:\TEST\_TESTFILER\good_file.txt' lrecl=175 recfm=V;
	input;
	if length(_infile_)<175 then 
		do;
			_infile_=tranwrd(_infile_,'0d0a'x,"  ");
	  	end;
    put _infile_ @;
run;

When I work with bad_file in EditPlus, and delete CRLF and insert two
SPACE, just like I try to do with the above code, the records become
175 positions wide. But the code make them just 174 postitions wide.
How is that ?

Another problem is that the code has not substituted the CRLF in
good_file in record 7, positon 110. Why is that so ?

Regards 
Rune Runnest

7. Searching for CRLF in a file

8. Detecting CRLF in a file except those in the end of the records

The issue is to find if a file contains CRLF in the records. The
record length is 580. In position 581 and 582 is CRLF, but those don't
count. And I do not want the code to detect those CRLF's.

First, I thought this code would work, but it does not. Because I want
to detect CRLF in any position from 1 to 580.

data _null_;
    infile my_file   end=eof lrecl=1 recfm=V;
    input b $char1. ;
    a=lag(b); 
    if  a||b='0d0a'x then 
		n+1;
    if eof then 
		do;
           	call symput('num_CRLF',put(n,best12.));
        end;
run;

Will this code work ? I have written "?" where I am not sure of the
right syntax.

data _null_;
  	infile my_file end=eof  lrecl=??   recfm=???;
  	length linje $580.;
	do until eof;
		if scan (linje,????) then *search from 1 to 580;
			do;
				n+1;
			end;
		if eof then 
			do;
				call symput ('num_CRLF', put(n,best12.));
			end;
run;




Regards 
Rune Runnestoe