BookmarkSubscribeRSS Feed
jcauteru
Calcite | Level 5

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?

3 REPLIES 3
Tom
Super User Tom
Super User

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;

jcauteru
Calcite | Level 5

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.

Hima
Obsidian | Level 7

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!!!

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 1026 views
  • 0 likes
  • 3 in conversation