sas proc import

Reply
Super Contributor
Posts: 371

sas proc import

when you use a sas proc import with the option getnames=yes

If you have a text file likes this :

v1;v2;; or v1;v2;

12;15;;

I want to avoid the error created by the missing of the name of the variable.

Super User
Posts: 11,105

Re: sas proc import

You should post the code and some example data and log showing the text of any error messages.

Generally I do not get an error when importing with a missing name but do end up with a variable named something like VARNN.

Sometimes I get warnings if the variable name is real long then there will be a note but not an error.

What kind of output do you expect?

If the files are CSV you may have to write data step code to read as desired.

Super Contributor
Posts: 371

Re: sas proc import

Thank you for your message.

I have a text file .txt, I want to import it by using a sas proc import

file.txt

v1;v2;v3;

kk;12;4;

mm;45;6;

Super User
Super User
Posts: 6,842

Re: sas proc import

Works easily for me. The extra semicolon just causes IMPORT to create an extra empty column.  VAR4 in this case.

filename example temp;

data _null_;

  file example;

  put

  'v1;v2;v3;'

/'kk;12;4;'

/'mm;45;6;'

  ;

run;

proc import datafile=example dbms=dlm out=example replace ;

  delimiter=';';

run;

proc print data=example;

run;

Super Contributor
Posts: 371

Re: sas proc import

Thank you tom.

That's correct

But the problem is that, I have a big file, so if I write

put

....

....

It can be very long ?

filename example temp;

data _null_;

  file example;

  put

  'v1;v2;v3;'

/'kk;12;4;'

/'mm;45;6;'

  ;

run;

Super User
Posts: 9,856

Re: sas proc import

How about first proc import, then drop these void variables ?

Super Contributor
Posts: 371

Re: sas proc import

Thank you.

I will try it

Super User
Super User
Posts: 6,842

Re: sas proc import

You said that you already HAVE a text file so there is no need to repeat the step I did to create an example file.

Did you try the PROC IMPORT step with your actual file(s)?

It should work without any errors.

Super Contributor
Posts: 371

Re: sas proc import

Did you try the PROC IMPORT step with your actual file(s)?

===> Yes, I have used the proc import with  getnames=yes, but it does not give the good results.


It should work without any errors.

===>  Yes, that's exact. It does not give any errors

Super User
Super User
Posts: 6,842

Re: sas proc import

So you need to paste the SAS log from the PROC IMPORT statement. If it is not listing any errors can you explain why you think that it "did not work".

Here is what my SAS log looks like for the example three line data file.

213   proc import datafile=example dbms=dlm out=example replace ;

214     delimiter=';';

215   run;

Number of names found is less than number of variables found.

Number of names found is less than number of variables found.

216    /**********************************************************************

217    *   PRODUCT:   SAS

218    *   VERSION:   9.4

219    *   CREATOR:   External File Interface

220    *   DATE:      22JUL15

221    *   DESC:      Generated SAS Datastep Code

222    *   TEMPLATE SOURCE:  (None Specified.)

223    ***********************************************************************/

224       data WORK.EXAMPLE    ;

225       %let _EFIERR_ = 0; /* set the ERROR detection macro variable */

226       infile EXAMPLE delimiter = ';' MISSOVER DSD lrecl=32767 firstobs=2 ;

227          informat v1 $2. ;

228          informat v2 best32. ;

229          informat v3 best32. ;

230          informat VAR4 $1. ;

231          format v1 $2. ;

232          format v2 best12. ;

233          format v3 best12. ;

234          format VAR4 $1. ;

235       input

236                   v1 $

237                   v2

238                   v3

239                   VAR4 $

240       ;

241       if _ERROR_ then call symputx('_EFIERR_',1);  /* set ERROR detection macro variable */

242       run;

NOTE: The infile EXAMPLE is:

      Filename=C:\Users\...\#LN00062,

      RECFM=V,LRECL=32767,File Size (bytes)=31,

      Last Modified=25Jul2015:20:04:07,

      Create Time=25Jul2015:20:04:07


NOTE: 2 records were read from the infile EXAMPLE.

      The minimum record length was 8.

      The maximum record length was 8.

NOTE: The data set WORK.EXAMPLE has 2 observations and 4 variables.

NOTE: DATA statement used (Total process time):

      real time           0.01 seconds

      cpu time            0.01 seconds

2 rows created in WORK.EXAMPLE from EXAMPLE.

NOTE: WORK.EXAMPLE data set was successfully created.

NOTE: The data set WORK.EXAMPLE has 2 observations and 4 variables.

NOTE: PROCEDURE IMPORT used (Total process time):

      real time           0.18 seconds

      cpu time            0.14 seconds

Super User
Posts: 9,856

Re: sas proc import

1) First getnames=no , then change variables' name.

2) An alternative way is using DATA STEP to import data.

3) F4 to copy the code generated by proc import from LOG, then get rid of option  DSD .

Message was edited by: xia keshan

Super Contributor
Posts: 371

Re: sas proc import

Thank you for your message.

You are right,

2) An alternative way is using DATA STEP to import data.

it is a good idea, some time, i use it.

But the problem is that, the order of variable change, I must have to change the code and I want an automatic solution for all type of data

Super User
Super User
Posts: 6,842

Re: sas proc import

You still need to explain why you think that the PROC IMPORT is not working. 

Is it that your source files do not look like the example you posted and so PROC IMPORT just fails with some type of error message?  If so what error message?

Is it that PROC IMPORT is not able to "guess" the proper data type for your variables?  If so then that is just a general problem with using a data format (CSV or Delimited file) that does not contain any information about the data types it contains.  PROC IMPORT will not be able to solve that issue.

If there is some other issue with the dataset that it generates, can you explain what the issue is?

If the only trouble is that your input files have variables appearing in different order than perhaps it would be easier to fix this at the source instead?  Have the people or system that genrates the files generate them in a defined format.

Or if the variable names are consistent and you know based on the name what type of data the column will contain then you can read the header line and use the names to merge with information about the data formats for the known variables and generate a data step to read the values.

Ask a Question
Discussion stats
  • 12 replies
  • 390 views
  • 0 likes
  • 4 in conversation