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;
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;
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.
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;
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
);
Dive into keynotes, announcements and breakthroughs on demand.
Explore Now →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.
Ready to level-up your skills? Choose your own adventure.