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

Is there a way to modify a variable format from numeric to string using proc import ?
If so please provide an example.

proc import out=bandep.&co._auto_prm&moisc.&year._&suffix1.
     datafile="&prefix3/&co./&co._auto_prm&moisc.&year..csv"    
     DBMS=CSV
     REPLACE;
     DELIMITER='|';
     GETNAMES=YES;
     GUESSINGROWS=MAX;
RUN;
1 ACCEPTED SOLUTION

Accepted Solutions
Kathryn_SAS
SAS Employee

Here is an example:

/* create a csv file of the sashelp.class data set */
ods listing close;
ods csv file='c:\temp\class.csv';

proc print data=sashelp.class noobs;
run;

ods csv close;
ods listing;

%let EFI_ALLCHARS=yes;
proc import datafile='c:\temp\class.csv'
 out=class dbms=csv replace;
run;

proc contents data=class;
run;

View solution in original post

5 REPLIES 5
Kathryn_SAS
SAS Employee

If you want to import a variable as character rather than numeric from a CSV file, you have several options:

1. PROC IMPORT determines a variable's type by the first 20 rows. Add a dummy first row that has a character value for the desired variable.

2. Import ALL variables as character by adding the following statement before PROC IMPORT:

%let EFI_ALLCHARS=YES;

3. Recall the DATA Step code that is run behind the scenes and then modify the INPUT, INFORMAT, and FORMAT statements for the desired variable.

alepage
Barite | Level 11
Can you provide a SAS code example because I not famillar with %let EFI_ALLCHAR=yes;
Kathryn_SAS
SAS Employee

Here is an example:

/* create a csv file of the sashelp.class data set */
ods listing close;
ods csv file='c:\temp\class.csv';

proc print data=sashelp.class noobs;
run;

ods csv close;
ods listing;

%let EFI_ALLCHARS=yes;
proc import datafile='c:\temp\class.csv'
 out=class dbms=csv replace;
run;

proc contents data=class;
run;

Tom
Super User Tom
Super User

Easiest answer is to skip PROC IMPORT and just write the data step to read the file yourself.

It is really not that hard and in fact the code might actually be easier than the code to write PROC IMPORT.

Example:

data bandep.&co._auto_prm&moisc.&year._&suffix1.;
  infile "&prefix3/&co./&co._auto_prm&moisc.&year..csv" 
    dsd truncover firstobs=2 dlm='|';
  length var1 var2 var3 var4 8 var5 $10 var6 $5 ;
  input var1--var6;
run;

Or if you do need a tool that can GUESS how to define the variables, but also allow you to override those decisions you could use this macro instead:  %csv2ds() 

 

Say you had the same 6 column CSV file as implied by the first example code above and you wanted to force the fifth variable to be character with length of 10.

 

data override;
  varnum=5;
  length='$10';
  informat=' ';
  format=' ';
run;
%csv2ds
(out=bandep.&co._auto_prm&moisc.&year._&suffix1.
,filen="&prefix3/&co./&co._auto_prm&moisc.&year..csv"
,dlm='|'
,replace=1
,overrides=override
);

 

Kurt_Bremser
Super User
When reading text files, don‘t waste your time with PROC IMPORT. Write the DATA step yourself, according to the documentation of the file.
You‘ll get much cleaner and easier to maintain code, and have the full flexibility if DATA step coding at your hand.
The accepted solution will make it impossible to correctly read numeric values, including dates and times.

Catch up on SAS Innovate 2026

Dive into keynotes, announcements and breakthroughs on demand.

Explore Now →
Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 5 replies
  • 405 views
  • 2 likes
  • 4 in conversation