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!!
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;
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;
But it still does not work
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;
Thank you! I have it now.
so how do we actually know dlm is not space but tabs?
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.
notepad++ is another free editor which allows you to see all the hidden stuff
thanks
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.