I really think this other team just doesn't like me... they sent me a .log file today where the first 4 variables are tab delimited, then the next 10 variables are comma delimited. And I can't use an @PUT statement, because while the tab delimited variables are all 36 characters, the comma delimited variables are different lengths.
Anyone have a suggestion? I think I stumped google...
Thanks!
If it is just the case that all rows have the same number of values and it is using two different delimiters then you can tell SAS to use both tab and comma as a delimiter.
infile 'myfile' dsd dlm='092C'x .... ;
or
dlm=','||'09'x ;
infile 'myfile' dsd dlm=dlm ... ;
But if instead they have tried to store a hierarchical structure into a flat file where the a particular tab delimited field can have a different number of commas within it per observation then you will need to take a two step approach of reading the comma delimited values as character strings and then parsing out the individual values.
data want;
length fixed1 fixed2 fixed3 fixed4 8 text5-text10 $36 ;
infile myfile dsd dlm='09'x truncover ;
input fixed1-fixed4 text5-text10 ;
array var5 var51 - var59 ;
do i=1 by 9 ;
var5(i) = scan(text5,i,',');
end;
....
run;
filename x "path";
Data have;
infile x dlm='09'x dsd;
input var1 var2 var3 .......;
run;
Thanks,
Jagadish
Hi Jagadish,
That only is going to import the tab delimited. I need the comma delimited to be individual variables as well.
Thanks,
Russell
If it is just the case that all rows have the same number of values and it is using two different delimiters then you can tell SAS to use both tab and comma as a delimiter.
infile 'myfile' dsd dlm='092C'x .... ;
or
dlm=','||'09'x ;
infile 'myfile' dsd dlm=dlm ... ;
But if instead they have tried to store a hierarchical structure into a flat file where the a particular tab delimited field can have a different number of commas within it per observation then you will need to take a two step approach of reading the comma delimited values as character strings and then parsing out the individual values.
data want;
length fixed1 fixed2 fixed3 fixed4 8 text5-text10 $36 ;
infile myfile dsd dlm='09'x truncover ;
input fixed1-fixed4 text5-text10 ;
array var5 var51 - var59 ;
do i=1 by 9 ;
var5(i) = scan(text5,i,',');
end;
....
run;
Wow - thanks! Works perfect!
guys then how can we resolve this scenario...?
cards,
12,13|14,15|16,17
5|6|,7,8|9,10
90,20,|15,.,.|12
;
Thanks!
Depends on what you want that to mean.
Hi Sandy,
You can either use what Sandip said or if you want to use hexadecimal it would be '7C2C'x
Full list of hexadecimal codes can be found here: ASCII Codes in Hex, Octal, and Decimal
what i meant was, what if we have data file contains rendom delimiter for one observation ('|', ',') but not in same order for each observation and with same number of variables for all observation with some missing values for some observation too ? - how we read that kind of data?
i have shown example what i have in my mind...data like below...
cards,
12,13|14,15|16,17
5|6|,7,8|9,10
90,20,|15,.,.|12
;
Not sure what you mean. The following would work if your data represent 7 variables and the first record is simply missing the 7th variable:
data have;
infile cards dlm='|,' dsd truncover;
input x1-x7;
cards;
12,13|14,15|16,17
5|6|,7,8|9,10
90,20,|15,.,.|12
;
awesome sir, its working fine...
Thanks for clearing doubts...
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.