BookmarkSubscribeRSS Feed
KenD
Calcite | Level 5
I've got mixed characters in one of my columns in a CSV file I'm importing. I know that mixed=yes will work when importing an excel sheet, but I get an error when using this with a csv file. Does this feature just not work when using Proc Import on anything besides Excel? If so, what are my alternatives aside from writing a big input statement?

Thanks for any help!
1 REPLY 1
MikeZdeb
Rhodochrosite | Level 12
hi ... one idea, you don't have to write a LONG input statement
you just have to write the names of the variables that should be
CHARACTER in a LENGTH statement and the variables that should be
SAS DATES in an INFORMAT statement

this assumes that the first record in your CSV file contains the names of the variables separated by commas

assume this is TEST.CSV ...

name,age,zip,gender,dob
MIKE,63,12203,M,3/7/1947
SARA,32,20001,F,9/24/1976
ARMMONDO,99,12345,U,1/1/1960


* the CSV file;
filename x 'z:\test.csv';

* read 1st record, place variable names in a macro variable;
data _null_;
infile x obs=1;
input;
call symputx('vars',translate(_infile_,' ',','));
run;

*
read the data
add a LENGTH statement for only the character variables
arbitrarily assign a length of 20 to all character data
(changed later in code)
add an INFORMAT and FORMAT for all dates
;
data test;
length name zip gender $20;
informat dob mmddyy10.;

infile x firstobs=2 dsd;
input &vars;

format dob mmddyy10.;
run;

* what happened;
proc contents data=test;
ods select variables;
run;

*
find maximum length of all character variables
create a length statement as a macro variable
;
data _null_;
set test end=last;
array ch(*) _character_;
array mx(100) _temporary_;
length txt $500;
do j = 1 to dim(ch);
mx(j) = max(mx(j),length(ch(j)));
end;
if last then do;
do j=1 to dim(ch);
txt = catx(' ',txt,vname(ch(j)),'$',mx(j));
end;
call symputx('lengths',txt);
end;
run;

*
reread the data, change the length of the character variables
the retain restores the original variable order from the CSV file
;
data test;
retain &vars;
length &lengths;
set test;
run;

proc contents data=test;
ods select variables;
run;

proc print data=test;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1 reply
  • 4525 views
  • 1 like
  • 2 in conversation