BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
thdang
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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

9 REPLIES 9
art297
Opal | Level 21

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;

thdang
Calcite | Level 5

But it still does not work

Patrick
Opal | Level 21

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;

thdang
Calcite | Level 5

Thank you! I have it now.

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

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

art297
Opal | Level 21

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.

Patrick
Opal | Level 21

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

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

thanks

Tom
Super User Tom
Super User

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;

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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