<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Alternative to mixed=yes when using Proc Import on a CSV file in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Alternative-to-mixed-yes-when-using-Proc-Import-on-a-CSV-file/m-p/64118#M18212</link>
    <description>hi ... one idea, you don't have to write a LONG input statement&lt;BR /&gt;
you just have to write the names of the variables that should be&lt;BR /&gt;
CHARACTER in a LENGTH statement and the variables that should be&lt;BR /&gt;
SAS DATES in an INFORMAT statement&lt;BR /&gt;
&lt;BR /&gt;
this assumes that the first record in your CSV file contains the names of the variables separated by commas&lt;BR /&gt;
&lt;BR /&gt;
assume this is TEST.CSV ...&lt;BR /&gt;
&lt;BR /&gt;
name,age,zip,gender,dob&lt;BR /&gt;
MIKE,63,12203,M,3/7/1947&lt;BR /&gt;
SARA,32,20001,F,9/24/1976&lt;BR /&gt;
ARMMONDO,99,12345,U,1/1/1960 &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
* the CSV file;&lt;BR /&gt;
filename x 'z:\test.csv';&lt;BR /&gt;
&lt;BR /&gt;
* read 1st record, place variable names in a macro variable;&lt;BR /&gt;
data _null_;&lt;BR /&gt;
infile x obs=1;&lt;BR /&gt;
input;&lt;BR /&gt;
call symputx('vars',translate(_infile_,' ',','));&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
* &lt;BR /&gt;
read the data&lt;BR /&gt;
add a LENGTH statement for only the character variables&lt;BR /&gt;
arbitrarily assign a length of 20 to all character data&lt;BR /&gt;
(changed later in code)&lt;BR /&gt;
add an INFORMAT and FORMAT for all dates&lt;BR /&gt;
;&lt;BR /&gt;
data test;&lt;BR /&gt;
length name zip gender $20;&lt;BR /&gt;
informat dob mmddyy10.;&lt;BR /&gt;
&lt;BR /&gt;
infile x firstobs=2 dsd;&lt;BR /&gt;
input &amp;amp;vars;&lt;BR /&gt;
&lt;BR /&gt;
format dob mmddyy10.;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
* what happened;&lt;BR /&gt;
proc contents data=test;&lt;BR /&gt;
ods select variables;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
* &lt;BR /&gt;
find maximum length of all character variables&lt;BR /&gt;
create a length statement as a macro variable&lt;BR /&gt;
;&lt;BR /&gt;
data _null_;&lt;BR /&gt;
set test end=last;&lt;BR /&gt;
array ch(*) _character_;&lt;BR /&gt;
array mx(100) _temporary_;&lt;BR /&gt;
length txt $500;&lt;BR /&gt;
do j = 1 to dim(ch);&lt;BR /&gt;
   mx(j) = max(mx(j),length(ch(j)));&lt;BR /&gt;
end;&lt;BR /&gt;
if last then do;&lt;BR /&gt;
   do j=1 to dim(ch);&lt;BR /&gt;
      txt = catx(' ',txt,vname(ch(j)),'$',mx(j));&lt;BR /&gt;
   end; &lt;BR /&gt;
   call symputx('lengths',txt);&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
* &lt;BR /&gt;
reread the data, change the length of the character variables&lt;BR /&gt;
the retain restores the original variable order from the CSV file&lt;BR /&gt;
;&lt;BR /&gt;
data test;&lt;BR /&gt;
retain &amp;amp;vars;&lt;BR /&gt;
length &amp;amp;lengths;&lt;BR /&gt;
set test;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc contents data=test;&lt;BR /&gt;
ods select variables;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc print data=test;&lt;BR /&gt;
run;</description>
    <pubDate>Wed, 03 Mar 2010 15:44:53 GMT</pubDate>
    <dc:creator>MikeZdeb</dc:creator>
    <dc:date>2010-03-03T15:44:53Z</dc:date>
    <item>
      <title>Alternative to mixed=yes when using Proc Import on a CSV file</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Alternative-to-mixed-yes-when-using-Proc-Import-on-a-CSV-file/m-p/64117#M18211</link>
      <description>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?&lt;BR /&gt;
&lt;BR /&gt;
Thanks for any help!</description>
      <pubDate>Wed, 03 Mar 2010 15:09:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Alternative-to-mixed-yes-when-using-Proc-Import-on-a-CSV-file/m-p/64117#M18211</guid>
      <dc:creator>KenD</dc:creator>
      <dc:date>2010-03-03T15:09:27Z</dc:date>
    </item>
    <item>
      <title>Re: Alternative to mixed=yes when using Proc Import on a CSV file</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Alternative-to-mixed-yes-when-using-Proc-Import-on-a-CSV-file/m-p/64118#M18212</link>
      <description>hi ... one idea, you don't have to write a LONG input statement&lt;BR /&gt;
you just have to write the names of the variables that should be&lt;BR /&gt;
CHARACTER in a LENGTH statement and the variables that should be&lt;BR /&gt;
SAS DATES in an INFORMAT statement&lt;BR /&gt;
&lt;BR /&gt;
this assumes that the first record in your CSV file contains the names of the variables separated by commas&lt;BR /&gt;
&lt;BR /&gt;
assume this is TEST.CSV ...&lt;BR /&gt;
&lt;BR /&gt;
name,age,zip,gender,dob&lt;BR /&gt;
MIKE,63,12203,M,3/7/1947&lt;BR /&gt;
SARA,32,20001,F,9/24/1976&lt;BR /&gt;
ARMMONDO,99,12345,U,1/1/1960 &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
* the CSV file;&lt;BR /&gt;
filename x 'z:\test.csv';&lt;BR /&gt;
&lt;BR /&gt;
* read 1st record, place variable names in a macro variable;&lt;BR /&gt;
data _null_;&lt;BR /&gt;
infile x obs=1;&lt;BR /&gt;
input;&lt;BR /&gt;
call symputx('vars',translate(_infile_,' ',','));&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
* &lt;BR /&gt;
read the data&lt;BR /&gt;
add a LENGTH statement for only the character variables&lt;BR /&gt;
arbitrarily assign a length of 20 to all character data&lt;BR /&gt;
(changed later in code)&lt;BR /&gt;
add an INFORMAT and FORMAT for all dates&lt;BR /&gt;
;&lt;BR /&gt;
data test;&lt;BR /&gt;
length name zip gender $20;&lt;BR /&gt;
informat dob mmddyy10.;&lt;BR /&gt;
&lt;BR /&gt;
infile x firstobs=2 dsd;&lt;BR /&gt;
input &amp;amp;vars;&lt;BR /&gt;
&lt;BR /&gt;
format dob mmddyy10.;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
* what happened;&lt;BR /&gt;
proc contents data=test;&lt;BR /&gt;
ods select variables;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
* &lt;BR /&gt;
find maximum length of all character variables&lt;BR /&gt;
create a length statement as a macro variable&lt;BR /&gt;
;&lt;BR /&gt;
data _null_;&lt;BR /&gt;
set test end=last;&lt;BR /&gt;
array ch(*) _character_;&lt;BR /&gt;
array mx(100) _temporary_;&lt;BR /&gt;
length txt $500;&lt;BR /&gt;
do j = 1 to dim(ch);&lt;BR /&gt;
   mx(j) = max(mx(j),length(ch(j)));&lt;BR /&gt;
end;&lt;BR /&gt;
if last then do;&lt;BR /&gt;
   do j=1 to dim(ch);&lt;BR /&gt;
      txt = catx(' ',txt,vname(ch(j)),'$',mx(j));&lt;BR /&gt;
   end; &lt;BR /&gt;
   call symputx('lengths',txt);&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
* &lt;BR /&gt;
reread the data, change the length of the character variables&lt;BR /&gt;
the retain restores the original variable order from the CSV file&lt;BR /&gt;
;&lt;BR /&gt;
data test;&lt;BR /&gt;
retain &amp;amp;vars;&lt;BR /&gt;
length &amp;amp;lengths;&lt;BR /&gt;
set test;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc contents data=test;&lt;BR /&gt;
ods select variables;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc print data=test;&lt;BR /&gt;
run;</description>
      <pubDate>Wed, 03 Mar 2010 15:44:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Alternative-to-mixed-yes-when-using-Proc-Import-on-a-CSV-file/m-p/64118#M18212</guid>
      <dc:creator>MikeZdeb</dc:creator>
      <dc:date>2010-03-03T15:44:53Z</dc:date>
    </item>
  </channel>
</rss>

