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

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
Obsidian | Level 7

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: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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