BookmarkSubscribeRSS Feed
RandyStan
Fluorite | Level 6

Dear All:

 

Var_A          Var_B         Var_C        Var_D

1003              1000Z           ACZ         PET

1006           400BA             PQR        DON

 

The Code I wrote:

Infile 'C:\Temp\have.csv' DLM = ','
Lrecl = 32000 DSD Truncover firstobs = 1; Input Var_A Var_B

Var_C   Var_D  $50.; run;

 

 

What is the mistake that I am making?

    Thank you in advance

1 REPLY 1
Tom
Super User Tom
Super User

Show the log.  And remember when posting text (like lines of data or lines from the SAS log or SAS program code) make sure to use the Insert Code or Insert SAS Code buttons in the editor menu to get a Pop-up window where you can paste the text (use the same button to edit the text if you need to make changes).

 

You need a DATA statement before you can use statements like those.

If the second column has letters in it then you cannot read it as if it was a number like you tried.

Do you actually have a CSV file?  It is hard to tell because you posted something that looks like a table instead. A CSV file is a text file.  So look at it with a text editor to see what it contains.  It should have commas and not then spaces that you showed between the values.

 

Why did you set the LRECL to 32000?  The default is 32767.  And if your file only has four columns that are each less than 50 bytes long there should be any need to use something other than the default.

 

Usually with a CSV you want to use the DSD option so that empty values and quoted values are read properly.  The default delimiter when you use the DSD is a comma so you don't need to specify it unless it actually is something else.  (In some places they like to use semicolon as the delimiter because the use comma for the decimal place in numbers, so their CSV Files are really SCSV files.)

 

Usually a CSV file will have a header row/line that you will to skip. So you need to add the FIRSTOBS=2 option to skip that header row.  If the file does not have a header row then there is no need to specify FIRSTOBS=1 as that is the normal behavior when reading a file.

 

If you want to use an in-line informat in the INPUT statement when reading in LIST MODE you need to prefix the informat with the : modifier so that only the next field on the line is read. Otherwise formatted mode will be used and it will either read through the delimiter, or stop before the end of the field if the width on the informat is shorter than the length of the field in the line.

 

If you want to read all 4 variables using the same informat you can use () to group the list of variables and list of formats to use.

 

data want;
  infile 'C:\Temp\have.csv' dsd truncover ;
  input (Var_A Var_B Var_C Var_D) (:$50.); 
run;

Or you could DEFINE the variables before the INPUT statement. Then the INPUT statement just needs to list the variab

data want;
  infile 'C:\Temp\have.csv' dsd truncover ;
  length Var_A Var_B Var_C Var_D $50 ;
  input Var_A Var_B Var_C Var_D ;
run;

 

 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1 reply
  • 304 views
  • 0 likes
  • 2 in conversation