BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SASTalk
SAS Employee
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
1 ACCEPTED SOLUTION

Accepted Solutions
Peter_C
Rhodochrosite | Level 12
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 (col:)(:) ;
put (col:)(:) ;
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

View solution in original post

4 REPLIES 4
GertNissen
Barite | Level 11
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.
GertNissen
Barite | Level 11
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;
Peter_C
Rhodochrosite | Level 12
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 (col:)(:) ;
put (col:)(:) ;
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
SASTalk
SAS Employee
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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 889 views
  • 0 likes
  • 3 in conversation