BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi All,

I am trying to automate a process of reading from reports that are not really formatted in a great way but am failing miserably at it. Can anyone help me? The data is in a format roughly like this:

REPORT XXX
STATION: ORD
==================
REPORT HEADERS
REPORT HEADERS2
(data starts below)
Origin1 sales2008 sales 2007
Origin2 sales2008 sales 2007
Origin3 sales2008 sales 2007
.
.
.
.
------------------
TTL

This repeats for 40 stations all in the same file. The number of origins can change and is not fixed. What I would like to do is:

1) Read in station name
2) read in top 5 origins
3) read in total (ttl above)
4) Go to the next station.

Any help would be greatly appreciated (and help me learn more about SAS).

Many thanks .
2 REPLIES 2
LinusH
Tourmaline | Level 20
This is handled by some classic data step programming. Could be done i thousands of ways, here's one. This example involves some help variables, retain, if-then-else and scans. If you are going to do more of this kind est you should consider taking a SAS programming class.

data report;
infile '~/reportdat.dat';
input;
length station origin $10
originCount 8
originNext $1;
retain station originCount originNext;
firstVar = scan(_infile_,1,' ');
if firstVar eq 'STATION:' then do;
station = scan(_infile_,2,' ');
originCount = 0;
originNext = 'N';
end;
else if firstVar eq '(data' then originNext = 'Y';
else if firstVar eq 'TTL' or
originNext = 'Y' and originCount lt 5 and firstVar not =: '-' then do;
origin = firstVar;
sales2008 = input(scan(_infile_,2,' '),best.);
sales2007 = input(scan(_infile_,3,' '),best.);
if firstVar eq 'TTL' then originNext = 'N';
else originCount+1;
output;
end;
drop firstVar originCount originNext;
run;

Regards,
Linus
Data never sleeps
deleted_user
Not applicable
Thanks so much for the tip Linus. You have opened my eyes. It is sure great learning tricks like this from other users.

All the best.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 2 replies
  • 743 views
  • 0 likes
  • 2 in conversation