Help using Base SAS procedures

how to import txt file

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 110
Accepted Solution

how to import txt file

Hi all,

i have a small doubt. There is one txt file.

To import it into  sas i used proc import method.

proc import datafile="D:\sample.txt"

out=ds

dbms=dlm

replace;

delimiter='09'X;

run;

By using this method some of the variables length is missing.

Suppose between variables here i mention i mention two variables. i.e. company name and sub branch company name.

Both are character variables.

sub branch length is 40 and it reads it completely.

But company name variable length is missing. its taking only 10.of

I don't know why its taken like that.

Is there any method to while reading the data from txt file to mention the length of variable. Is this possible in Proc Import.


Accepted Solutions
Solution
3 weeks ago
Super User
Super User
Posts: 6,153

Re: how to import txt file

[ Edited ]

Why are you using PROC IMPORT?

If you know the data then just write a data step to read the file.

Are you sure the file is tab delimited? Is it possible it is just a text file and the text editor has inserted random tabs where it thinks it can save space by replacing multiple spaces with a tab?

 

See: Reading Raw Data with the INPUT statement.

View solution in original post


All Replies
Esteemed Advisor
Esteemed Advisor
Posts: 6,996

Re: how to import txt file

Hi,

As far as I am aware there is no method in proc import to set lengths.  The reason is that proc import is a wrapper.  Behind the scenes it is guessing what your data is and generating a datastep import program.  This import program is normally written to the log for text files, so you can take that code that is generated and modify it as you want.  IMO its not a good idea to use proc import as it "guesses" what its to do.  You know the data so are best placed to tell the code what to do. 

There may also be a couple of other things you want to be sure of, is the delimiter really 09X?  Does the strings contain special characters etc.  I would recommend a CSV format file with quoted strings to be safer, or if you want to go the whole hog, then XML.  Are you saving from Excel perhaps?  If so then you can just save as CSV from there.

Respected Advisor
Posts: 3,773

Re: how to import txt file

Would adding the GUESSING rows option/statement fix your problem.  Have you looked at the documentation for PROC IMPORT?

Frequent Contributor
Posts: 110

Re: how to import txt file

GUESSING ROWS is worked in excel. here the data is tab delimited data. if the two variables having large size then some of the variables length is missing

so using proc import it is possible to mention length of variables while importing.

Respected Advisor
Posts: 3,773

Re: how to import txt file

You need to RTM.

The GUESSINGROWS statement is valid only for delimited files.

Frequent Contributor
Posts: 117

Re: how to import txt file

try using CSV as dbms. If this doesn't work , i think you need to check the data and why its getting truncated.

proc import datafile="D:\sample.txt"

out=ds

dbms=csv

replace;

delimiter='09'X;

run;

Frequent Contributor
Posts: 110

Re: how to import txt file

i used your method vish33  Sir but same result i am getting. Although there is no changes in data using dbms=tab or  dbms=csv.

variables length is missing. all are character variables.

Is there any efficient method that while importing mention variables size.

Solution
3 weeks ago
Super User
Super User
Posts: 6,153

Re: how to import txt file

[ Edited ]

Why are you using PROC IMPORT?

If you know the data then just write a data step to read the file.

Are you sure the file is tab delimited? Is it possible it is just a text file and the text editor has inserted random tabs where it thinks it can save space by replacing multiple spaces with a tab?

 

See: Reading Raw Data with the INPUT statement.

Frequent Contributor
Posts: 110

Re: how to import txt file

Thank u for your valid comments. I follow data step infile statement to read the full length of variable. I used  colon":" before of every variable. Ex: company_name: $50.

So i got the result. Once again thank u all for spending time for my post.

Grand Advisor
Posts: 16,925

Re: how to import txt file

Please read

Esteemed Advisor
Posts: 5,156

Re: how to import txt file

Delimiter option is not valid for DBMS=CSV, for obvious reasons.

'09'X is TAB. Try that as DBMS.

If that is not working for you, consider to use the data step instead, were you have complete control of field length etc.

Data never sleeps
Frequent Contributor
Posts: 77

Re: how to import txt file

This is not an import but it reads txt files.   My normal record is 77 bytes long;    Jim

data master;

    infile '/folders/myfolders/rawfiles/pre44.txt'

     obs=99 length=rli  dlm=’09’x;

  input dat: $varying200. rli @ ;

        x=rli;   put _all_;

       IF X = 77 THEN DO;

         input @5 cat $10.  @15 desc $30. @47 yr $10.

                  @54 c7  $8.  @64  c8 $8.    @70 c10 $8.;

       END;

        run;

proc print;

run;

Respected Advisor
Posts: 3,773

Re: how to import txt file

When reading delimited files formatted input is not appropriate. 

Super Contributor
Posts: 282

Re: how to import txt file

Hi,

Perhaps attach the sample.txt file (or a smaller version of it if it is large), so that we can see the data you're trying to read and show us what output you're seeing.

When you say "But company name variable length is missing. its taking only 10.of" Do you mean only the first 10 characters of company name are being read or only 10 fields are being read, or something else?

Regards,

Amir.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 13 replies
  • 3543 views
  • 0 likes
  • 9 in conversation