Help using Base SAS procedures

proc imfort format

Accepted Solution Solved
Reply
Super Contributor
Posts: 297
Accepted Solution

proc imfort format

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;


Accepted Solutions
Solution
‎06-19-2017 10:23 AM
Super User
Super User
Posts: 6,498

Re: proc imfort format

[ Edited ]

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;

View solution in original post


All Replies
PROC Star
Posts: 549

Re: proc imfort format

Do you mean that you want to import the CSV file into SAS with all variables defined as character variables?

Super Contributor
Posts: 297

Re: proc imfort format

yes

PROC Star
Posts: 549

Re: proc imfort format

[ Edited ]

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.

Super User
Posts: 6,928

Re: proc imfort format

Take the data step (that was created by proc import) from the log and adapt it to your needs.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super Contributor
Posts: 297

Re: proc imfort format

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. 

Super User
Posts: 6,928

Re: proc imfort format


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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎06-19-2017 10:23 AM
Super User
Super User
Posts: 6,498

Re: proc imfort format

[ Edited ]

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;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 184 views
  • 1 like
  • 4 in conversation