BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Taliah
Quartz | Level 8

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User
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.

Taliah
Quartz | Level 8

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

 

Kurt_Bremser
Super User

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.

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1147 views
  • 0 likes
  • 2 in conversation