Hi,
Is there a code to know the number of columns in a csv file?
We are using a program that inputs csv files. There are sometimes errors in the input files, were they have fewer columns then they need to have. I am looking for a way to check the number of columns. If less/more then required, I will write a code to send a warning (that is not a problem). Thank you.
Old habits die hard 😉
Since I mainly used INFILE in my professional work for fixed-length files (RECFM=F) where thde default LRECL is 256, I tend to always set it.
But for variable-length files, the default is 32767, so no separate LRECL is needed.
data _null_;
infile "path_to_your_file" lrecl=1000; /* set lrecl to sufficient value */
input;
call symputx("no_of_columns",countw(_infile_,","));
stop;
run;
The macro variable will contain the number of column names found in the header line.
great, Thank you!
I see the code you sent works with and without lrecl. Do you know if I can leave that out?
in the input code I use:
data test;
infile "[path and file] " dlm="," firstobs=1;
length a b c ...8;
input a b c...;
format a b c ... best12.;
run;
the code you sent to check number of columns works fine the way you sent it and without lrecl
Thank you
Old habits die hard 😉
Since I mainly used INFILE in my professional work for fixed-length files (RECFM=F) where thde default LRECL is 256, I tend to always set it.
But for variable-length files, the default is 32767, so no separate LRECL is needed.
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!
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.
Ready to level-up your skills? Choose your own adventure.