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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.