Hello:
I use proc import to import the .csv file into SAS. Based on some situation, I need to import all the variables of the whole file with Charateric strings. Is there a way to do this in the option of Proc mport? Or other ways? Please advice. Thanks.
proc import out=test
datafile="dictionary.csv"
dbms=csv replace;
getnames=yes;
guessingrows=159503;
quit;
If you just want to read everything as character then it is probably much easier to just write your own data step instead of asking PROC IMPORT to guess at how to write the data step.
If you don't care (or already know) the variable names then the data step is trivial.
data test;
infile "dictionary.csv" dsd firstobs=2 truncover ;
length var1-var400 $200 ;
input var1-var400 ;
run;
If you need to read the names from the file you could do that also.
filename code temp;
data _null_;
infile "dictionary.csv" obs=1 ;
input ;
file code ;
_infile_=translate(_infile_,' ',',');
put 'length ' _infile_ ' $200;' ;
run;
data test;
infile "dictionary.csv" dsd firstobs=2 truncover ;
%inc code /source2;
input (_all_) (+0);
run;
Do you mean that you want to import the CSV file into SAS with all variables defined as character variables?
yes
you can do this directly in a data step using the INFILE statement with the DSD option and specifying your variables as character variables in the INPUT statement.
Take the data step (that was created by proc import) from the log and adapt it to your needs.
My data set contains 400 variables. Is there a way I don't have list all of them? The fact to input all of variables as Charater is to keep the same format of the original file. We do't want SAS to change anything by proc import default.
@ybz12003 wrote:
My data set contains 400 variables. Is there a way I don't have list all of them? The fact to input all of variables as Charater is to keep the same format of the original file. We do't want SAS to change anything by proc import default.
So what?
The data step created by proc import already contains all 400 variables. Just look for those that are read as numeric and change them to character.
I also do not see any value to "keep the format". You already migrate from text to a SAS dataset, so you should keep numbers as numeric where appropriate.
If you have numeric values throughout a column that needs to be kept as character (something which happens quite often, eg with 2-digit numbers or strings that are actually keys), then the manually written data step will always be your best option.
Keep in mind that you write that data step once, but will use it multiple times. And you save yourself from the vagaries of proc import, which after all makes just guesses.
If you just want to read everything as character then it is probably much easier to just write your own data step instead of asking PROC IMPORT to guess at how to write the data step.
If you don't care (or already know) the variable names then the data step is trivial.
data test;
infile "dictionary.csv" dsd firstobs=2 truncover ;
length var1-var400 $200 ;
input var1-var400 ;
run;
If you need to read the names from the file you could do that also.
filename code temp;
data _null_;
infile "dictionary.csv" obs=1 ;
input ;
file code ;
_infile_=translate(_infile_,' ',',');
put 'length ' _infile_ ' $200;' ;
run;
data test;
infile "dictionary.csv" dsd firstobs=2 truncover ;
%inc code /source2;
input (_all_) (+0);
run;
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.