Home > front end >  Choose the format of variables while importing csv file
Choose the format of variables while importing csv file

Time:11-11

I'm new to SAS and I wish to import a csv file. This file has a column containing characters starting with a 0 (for instance, 01000 or 05200) and is 5 character long.

When I open my file with a calc software, no problem. But when I import in SAS with:

proc import file="myfile.csv"
    out=output
    dbms=csv;
run;

The column is then considered as numerical, and so the first 0 gets deleted. Changing the format afterwards doesn't solve my problem.

Is there a solution to specify the format import prior the csv reading, or just a solution to force the import of all the columns as characters?

Thanks a lot!

CodePudding user response:

The easiest solution is to read the file with a program instead of forcing SAS to guess how to read the file. PROC IMPORT will actually generate a program that you could use as a model. But it is not hard to write your own. Then you will have complete control over how the variables are defined: NAME; TYPE (numeric or character); storage LENGTH; LABEL; FORMAT to use for display; INFORMAT to use for reading the values from the line.

Just define the variables, attach any required formats and/or informats, and then read them. For example this step would read two numeric and two character variables from the file. I made one of the character variables have DATE values so you can see how you might attach format and/or informat to a variable that would require it. Most variables do not need either an informat nor a format attached to them as SAS knows how to read and write both numbers and character strings.

data output;
  infile "myfile.csv" dsd firstobs=2 truncover;
  length var1 $10 var2 8 var3 $30 var4 8;
  informat var4 date.;
  format var4 yymmdd10.;
  input var1 var2 var3 var4;
run;
  • Related