BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ybz12003
Rhodochrosite | Level 12

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

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

ybz12003
Rhodochrosite | Level 12

yes

PeterClemmensen
Tourmaline | Level 20

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.

ybz12003
Rhodochrosite | Level 12

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. 

Kurt_Bremser
Super User

@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.

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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