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

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 !

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

5 REPLIES 5
Haikuo
Onyx | Level 15

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

SAShole
Pyrite | Level 9

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?

Haikuo
Onyx | Level 15

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

art297
Opal | Level 21

: 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.

SAShole
Pyrite | Level 9

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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