DATA Step, Macro, Functions and more

Is there a proc import option to format everything as character

Reply
New Contributor
Posts: 4

Is there a proc import option to format everything as character

Hi, can't seem to find anything on this on the proc import page. I's there a proc import option which will import everything in a DS as a character var. of specific length?

Super User
Super User
Posts: 6,500

Is there a proc import option to format everything as character

A little more imformation would help.  What are you importing from?

If it is a CSV file then perhaps you can just skip proc import.

data want ;

  length col1-col20 $200;

  infile 'my.csv' dsd truncover firstobs=2;

  input col1-col20;

run;

New Contributor
Posts: 4

Is there a proc import option to format everything as character

Hi Tom,

Right now I have it set up to import any number of raw data files in a directory into their own respestive datasets. Hopefully the code below will show what I'm doing:

%macro import_all;

data file_list;

          length fname $45.;

          infile indata truncover;

          input fname $45.;

          call symput ('num_files',_n_);

run;

data file_list;

          set file_list;

          fname = compress(trim(fname));

          sasname = scan(tranwrd(fname,"-","_"),1,'.');

          %if &n_change = 1 %then %do;

                    sasname = compress(tranwrd(sasname,&find., &replace.));

          %end;

run;

%macro read_in;

%do j = 1 %to &num_files.;

data _null_;

set file_list;

if _n_ = &j. then do;

          call symput ('filein',fname);

          call symput ('name', sasname);

end;

run;

%put &filein.;

%put &name.;

          %if &state. = 1 %then %do;

                    proc import datafile = "&filepath.\&filein."

                              out = data.&name.

                              dbms = dlm

                              replace;

                              delimiter = &delim.;

                              getnames = yes;

                              guessingrows = 32767;

                    run;

          %end;

          %if &state. = 2 %then %do;

                    proc import datafile = "&filepath.\&filein."

                              out = data.&name.

                              dbms = &delim.

                              replace;

                              getnames = yes;

                              guessingrows = 32767;

                    run;

          %end;

%end;

%mend read_in;

There are quite a few delimited raw data files (~90) so manual operations aren't really an option. Really what a I need is a way to tell proc import to import all variables in file as say $20. I can't list the variables because they change from file to file...I messed around with a few ways to grab the variable names fro the raw files and list them in the import statement, but they have special charecters so that was giving me trouble. Obviously <getnames = yes> solves this issue for the import.

I may have just thought of a way to do this, but if you have any thoughts let me know.

Regular Contributor
Posts: 233

Is there a proc import option to format everything as character

My thought is kind of manual but it worked.

  • Select the data in the spread sheet and right click on "format cells" and select "Text".
  • Select the entire spread sheet again and right click on "Column Width" and enter the column width that you want.
  • Save the file.
  • Run below proc import code.

            If running on a server

                         PROC IMPORT OUT= T

                         DATAFILE= "C:\Documents and Settings\Book1.xls"

                         DBMS=XLS REPLACE;

                         RUN;

           If running on a locally

                         PROC IMPORT OUT= T

                         DATAFILE= "C:\Documents and Settings\Book1.xls"

                         DBMS=EXCEL REPLACE;

                         RUN;

Tested and working fine. Good luck!!!

Ask a Question
Discussion stats
  • 3 replies
  • 438 views
  • 0 likes
  • 3 in conversation