Happy Holidays,
How do I validate what delimiter is used in a text file?
I have a set of text files that need to be pipe delimited. I need a SAS program that checks the text file and confirms the delimiter is a "|".
I can not figure out how to do this.
This is how to create a list of all CSV files in a directory and the number of comma separated fields on the first line
data _null_;
length thisFile $200;
infile "&sasforum\datasets\*.csv" eov=firstLine filename=thisFile;
input;
file "&sasforum\datasets\csvList.txt";
if _n_=1 or firstLine then do;
fields = countw(_infile_, ",");
put fields z4. +1 thisFile;
firstLine = 0;
end;
run;
tested on a Windows system.
Thanks. I do not currently have access to my work PC but will try out your suggestions and let you know if it works for me.
This is how to create a list of all CSV files in a directory and the number of comma separated fields on the first line
data _null_;
length thisFile $200;
infile "&sasforum\datasets\*.csv" eov=firstLine filename=thisFile;
input;
file "&sasforum\datasets\csvList.txt";
if _n_=1 or firstLine then do;
fields = countw(_infile_, ",");
put fields z4. +1 thisFile;
firstLine = 0;
end;
run;
tested on a Windows system.
Thanks for the example. I do not currently have access to my work PC but will let you know once I have tried it.
Very slick piece of code!
This will definitely go into my utility directory.
Best,
Tom
You need to watch out for quoted delimiters and adjacent delimiters. Use the 'mq' modifier on COUNTW() function.
Try this example to see the differences.
filename sample1 temp;
data _null_;
file sample1 ;
put '1,,"this, is quoted",4,5'
/ '1,2,3,4,5'
/ '1,,3,4,5'
;
run;
data _null_;
infile sample1 dsd truncover;
length col1-col7 $20;
input col1-col7;
n1=countw(_infile_,',');
n2=countw(_infile_,',','q');
n3=countw(_infile_,',','mq');
put _infile_ / (n: col:) (=) / ;
run;
Thanks PGStats, This solution worked for me with slight adjustments.
I really appreciate your prompt reply.
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.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.