DATA Step, Macro, Functions and more

Importing a 'Stacked' Text File

Accepted Solution Solved
Reply
Contributor
Posts: 62
Accepted Solution

Importing a 'Stacked' Text File

I'm trying to import a text file that is stacked by a group. the first line of the file and every 77th row afterword is simply i header i wish to do away with.

There are seven fields in the file:

datelower_estmid_estupper_eststd_devsubsystemforecast

Attached is the file i'm trying to import. Any Hints?

Thank you !

Attachment

Accepted Solutions
Solution
‎11-09-2012 01:08 PM
Respected Advisor
Posts: 3,124

Re: Importing a 'Stacked' Text File

Here is one way:

data have (drop=dt);

infile "h:\temp\data.txt";

if mod(_n_,76) =1 then input;

else do;

       input  dt $ 1-8

        lower_est 18-34

        mid_est   43-59

        upper_est 67-84

        std_dev   101-117

        subsystem $ 120-125  

        forecast 147-148;

        date=input(compress(dt,' /'),yymmn6.);

      output;

end;

format date mmddyy10.;

;

run;

Haikuo

View solution in original post


All Replies
Solution
‎11-09-2012 01:08 PM
Respected Advisor
Posts: 3,124

Re: Importing a 'Stacked' Text File

Here is one way:

data have (drop=dt);

infile "h:\temp\data.txt";

if mod(_n_,76) =1 then input;

else do;

       input  dt $ 1-8

        lower_est 18-34

        mid_est   43-59

        upper_est 67-84

        std_dev   101-117

        subsystem $ 120-125  

        forecast 147-148;

        date=input(compress(dt,' /'),yymmn6.);

      output;

end;

format date mmddyy10.;

;

run;

Haikuo

Contributor
Posts: 62

Re: Importing a 'Stacked' Text File

Haikou can you help me to understand how your code works?

if mod(_n_,76) =1 then input;

else do;

       input  dt $ 1-8

        lower_est 18-34

        mid_est   43-59

        upper_est 67-84

        std_dev   101-117

        subsystem $ 120-125 

        forecast 147-148;

        date=input(compress(dt,' /'),yymmn6.);

      output;


is this like saying if the record is a multiple of 76 then do nothing else read in the record and output the record to the dataset. Also how is it that the first record is skipped over? because mod(1,76) = 1 is true, correct?

Respected Advisor
Posts: 3,124

Re: Importing a 'Stacked' Text File

Two key points:

1. SAS process data in a sequential way, one by one, unless being told otherwise. By this sense, it is harder for you to skip reading a record, this is why there is " if mod(_n_,76) =1 then input;"

2. But you can control whether to output it or not by explicitly spelling out "output" to those you want to keep. An explicit "output" statement will override the implicit one.


HTH,

Haikuo

PROC Star
Posts: 7,363

Re: Importing a 'Stacked' Text File

: the first line of the file and every 77th row afterword will have a value of mod(_n_,76) equal to 1.  Thus, for those records, they are read and then simply ignored.

The fields in all of the other records are read and then output.

Contributor
Posts: 62

Re: Importing a 'Stacked' Text File

Thanks Haikou, this is just what I was looking for Smiley Happy

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 194 views
  • 0 likes
  • 3 in conversation