Help using Base SAS procedures

prxparse

Reply
SAS Employee
Posts: 2

prxparse

I'm using prxparse to take a raw datafile comma delim and chage it to a tab delim file. My issue I am having when I run the find replace code, it replaces the comma's with \t as literal instead of as tab. Please advise:

Code:

data _null_;
file "&newfile" LRECL = 2000;
infile "&origfile" LRECL = 900 TRUNCOVER;
input name $ 1-999;
name = prxparse('s/,/\t/');
call prxchange(name,-1,_infile_);
put _infile_;
run;


Here is a snippet of what its doing:

2.2.2\t0\t0\t2008\t5\t5\t4\t0\t1\t439\t0\t0\t0\t5.2\t Message was edited by: SASTalk
SAS Employee
Posts: 174

Re: prxparse

try replacing \t with '09'x

Note: On ASCII systems (PC, UNIX, MAC, VMS) the hex representation of a TAB character is '09'x. On EBCDIC systems (VM, MVS, VSE) the hex representation of a TAB is '05'x.
SAS Employee
Posts: 174

Re: prxparse

This is another solution
* create test input data*;
data _null_;
file 'c:\temp\comdlm.txt';
put "Samuel B. Thompson" ',' "04/28/1995" ',' "Raleigh";
put "Suzy B. Thomspon" ',' "5/1/1993" ',' "Wake Forest";
run;

data info;
infile 'c:\temp\comdlm.txt' DSD dlm=',' truncover;
input name :$30. DOB :mmddyy8. city :$20.;
file 'c:\temp\tabdlm.txt' dlm='09'x;
put name :$30. DOB :mmddyy8. city :$20.;
run;
Valued Guide
Posts: 2,177

Re: prxparse

Posted in reply to GertNissen
When replacing commas with tabs, consider, protected embedded commas. For Example:-
First some sample data including an embedded comma, properly protected..[pre]DATA _null_ ;
input ;
file "your_file" ;
_infile_ = trim( _infile_ );
put _infile_ ;
list;cards4 ;
a,b,c,d,
1,"AB,c",3,";",5
,2,,4,
email,"Goodnight, J;Thompson, Samuel B.",3,4,5
1,,3,,5
;;;;[/pre] Next, derive the number of column headers. Which I would hope is an adequate indicator for the file[pre]data _null_ ;
infile "your_file" dsd dlm=',' lrecl=10000 col=c length=l ;
do col=1 by 1 until( c > l ) ;
input cname :$40. @@ ;
end ;
put "INFO: there are " col " columns" ;
call symputx( 'n_cols', col ) ;
stop ;
run ; [/pre] Finally ready to read, sensitive to commas embedded and protected among data values[pre]data _null_ ;
infile "your_file" dsd dlm=',' lrecl=100000 truncover;
file "your_file2" dsd dlm='09'x lrecl=100000 ;
length col1-col&n_cols $32767 ;
input (colSmiley Happy(Smiley Happy ;
put (colSmiley Happy(Smiley Happy ;
LIST;
run ;[/pre]although that has completed the work required, the following step conveniently displays the '09'x as the new column delimiters, with protected commas remaining among the data[pre]data _null_ ;
infile "your_file2" ;
INPUT @ ;
LIST;
run ;[/pre]
Hope that is clear and helps.

PeterC
SAS Employee
Posts: 2

Re: prxparse

PeterC,

Thanks for the reply. It looks like to me their are a few different ways to do this. prxparse is not working as I expected though. I ended up using translate although I like how your method protects the commas that are not delims.

This seemed to work:

global serverOS infdlm;
let origfile =
%let newfile =
%let serverOS = %substr(&sysscp,1,2);
/* Macro to set tab delimeter */
%macro setdlm;
/* Windows */
%if (&serverOS eq WI) %then %do;
%let infdlm='09'x;
%end;
/* Unix */
%else %if (&serverOS eq HP) %then %do;
%let infdlm='09'x;
%end;
/* zOS */
%else %if (&serverOS eq OS) %then %do;
%let infdlm='05'x;
%end;
%else %do;
%put ERROR: Invalid value specified for serverOS;
%end;

%mend setdlm;
%setdlm;

/* Datastep to turn comma delim file to tab delim */
/* on a Windows Unix MVS Operating System */
data _null_;
file "&newfile" LRECL = 5000;
infile "&origfile" LRECL = 5000 TRUNCOVER;
input;

_infile_ =translate(_infile_,&infdlm,',');
put _infile_;
run;
Ask a Question
Discussion stats
  • 4 replies
  • 185 views
  • 0 likes
  • 3 in conversation