Help using Base SAS procedures

Cannot import text file

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 75
Accepted Solution

Cannot import text file

Hi, I have the following codes to import a text file but it did not work. Can someone help me with this? I also include the file here

filename myFile "C:\Users\Desktop\SAS\Lq_factor.txt";

data sasdata.ps_lq_factor;

format dt yymmn6.;

infile myFile missover;

input @;

if anyalpha(_infile_) = 0 and length(trimn(_infile_)) > 0 then do;

     input dttxt $ Level_lq    non_traded_lq traded_lq;

     if length(trim(dttxt)) = 6 then do;

          dt = input(dttxt, yymmn6.);

          output;

          end;

     end;

drop dttxt;

run;

Thank You very much!!

Attachment

Accepted Solutions
Solution
‎04-06-2013 07:49 PM
Respected Advisor
Posts: 4,173

Re: Cannot import text file

It's not working because the delimiter in your file are Tabs and not Blanks.

Add "expandtabs" to Art's code and it will work.

infile myFile missover expandtabs;

Alternatively some code like below would also do the job:

data sasdata.ps_lq_factor;
  infile myFile truncover dlm=' ' expandtabs;

  format dt yymmn6.;
  input @1 dt ?? :yymmn6. @;

  if not missing(dt);

  input Level_lq:best32. non_traded_lq:best32. traded_lq:best32.;

run;

View solution in original post


All Replies
PROC Star
Posts: 7,467

Re: Cannot import text file

You were awfully close.  You just left out one line to account for when your condition wasn't met.  i.e.:

data sasdata.ps_lq_factor;

  format dt yymmn6.;

  infile myFile missover;

  input @;

  if anyalpha(_infile_) = 0 and length(trimn(_infile_)) > 0 then do;

    input dttxt $ Level_lq    non_traded_lq traded_lq;

    if length(trim(dttxt)) = 6 then do;

      dt = input(dttxt, yymmn6.);

      output;

    end;

  end;

  else input;

  drop dttxt;

run;

Frequent Contributor
Posts: 75

Re: Cannot import text file

But it still does not work

Solution
‎04-06-2013 07:49 PM
Respected Advisor
Posts: 4,173

Re: Cannot import text file

It's not working because the delimiter in your file are Tabs and not Blanks.

Add "expandtabs" to Art's code and it will work.

infile myFile missover expandtabs;

Alternatively some code like below would also do the job:

data sasdata.ps_lq_factor;
  infile myFile truncover dlm=' ' expandtabs;

  format dt yymmn6.;
  input @1 dt ?? :yymmn6. @;

  if not missing(dt);

  input Level_lq:best32. non_traded_lq:best32. traded_lq:best32.;

run;

Frequent Contributor
Posts: 75

Re: Cannot import text file

Thank you! I have it now.

Super Contributor
Super Contributor
Posts: 444

Re: Cannot import text file

so how do we actually know dlm is not space but  tabs?

PROC Star
Posts: 7,467

Re: Cannot import text file

You can use SAS to view a file's data in its hex representation.  I, personally, prefer to just view it using a free viewer, such as:

Download Free HexMad, HexMad 0.2.1 Download

I didn't, in this case, as I had just copied and pasted the text, and read it with a cards statement and never even knew (or had to know) that there were tab characters.

Respected Advisor
Posts: 4,173

Re: Cannot import text file

notepad++ is another free editor which allows you to see all the hidden stuff

Super Contributor
Super Contributor
Posts: 444

Re: Cannot import text file

thanks

Super User
Super User
Posts: 7,039

Re: Cannot import text file

Why not just skip the header?

data want ;

   infile 'myfile' dlm='09'x firstobs=13 truncover ;

   input month yymmn6. col2-col4 ;

   format month yymmn6.;

run;

If the header is variable in length then you can test for header lines and skip them.

data want ;

   infile 'myfile' dlm='09'x truncover ;

   input @ ;

   if _infile_ in: ('%','09'x) then delete ;

   input month yymmn6. col2-col4 ;

   format month yymmn6.;

run;

🔒 This topic is solved and locked.

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

Discussion stats
  • 9 replies
  • 308 views
  • 0 likes
  • 5 in conversation