Help using Base SAS procedures

reading in a file from specific rows

Reply
Contributor
Posts: 65

reading in a file from specific rows

I have a tab delimited file and basically I want to read row 4 (date value) and put it into a column. I also want to read only rows that have a valid time and data. Below is an example of the file and program:

# ----------------------------------------

Visits for all visitors
April 23, 2010 April 23, 2010 ********want this in a column
# ----------------------------------------

# ----------------------------------------
# Graph
# ----------------------------------------
Hour All Visits (Segment) Traffic (Segment) Traffic (Segment)
00:00:00 492 0 492
01:00:00 259 0 259
02:00:00 130 0 130
03:00:00 100 0 100
04:00:00 66 0 66
05:00:00 99 0 99
06:00:00 242 6 236
07:00:00 597 80 517
08:00:00 1,106 268 838
09:00:00 1,421 383 1,038
10:00:00 1,518 438 1,080
11:00:00 1,524 419 1,105
12:00:00 1,436 321 1,115
13:00:00 1,385 243 1,142
14:00:00 1,252 158 1,094
15:00:00 1,299 142 1,157
16:00:00 1,178 87 1,091
17:00:00 1,025 16 1,009
18:00:00 928 7 921
19:00:00 971 3 968
20:00:00 992 3 989
21:00:00 998 3 995
22:00:00 933 0 933
23:00:00 731 0 731
# -------------------------------------------------------------------------------- causing issues in log

data va;
infile 'D:\Temp\test1.csv' dlm='09'x FIRSTOBS=11;
INPUT Time $ Visits $ CampusTraffic $ OffCampusTraffic $ ;
Super User
Posts: 5,260

Re: reading in a file from specific rows

You could use if = _n_ 4 then... to assign your master date. Have this column in a retain statement.

To check whether a date is valid you could just input there value using appropriate date format.
Testing the other data depends on what is valid data. Non-missing? Using (subsetting) if statments seems to be the most straightforward way to do it.

/Linus
Data never sleeps
Valued Guide
Posts: 2,175

Re: reading in a file from specific rows

remove that firstobs=[pre]data va;
infile 'D:\Temp\test1.csv' dlm='09'x truncover ;
retain visitor_date '12345678901234567890' ;
if visitor_date =:'1234567890' then do ;
input @'Visits' ;
input ;
visit_date = _infile_ ;
delete;
end ;
input hour ? @ ;
if _error_ then do;
_error_ = 0 ;
delete ;
end ;
input allv trafS1 trafs2 ;
label allv='All Visits' trafs1= 'Campus Traffic' trafs2='Off-Campus Traffic' ;
format hour hhmm. ;
informat hour time8. allv trafs: comma14. ;
run ;[/pre]
good luck
PeterC
Super User
Posts: 9,687

Re: reading in a file from specific rows

My code is :

[pre]
data temp;
if _n_ =1 then do;
input @'visitors' / date $14. @'(Segment)' / Time $ Visits $ CampusTraffic $ OffCampusTraffic $ ;
output;
end;

input Time $ Visits $ CampusTraffic $ OffCampusTraffic $ ;
if Time ne '#' then output;
retain date;
datalines;
# ----------------------------------------

Visits for all visitors
April 23, 2010 April 23, 2010 ********want this in a column
# ----------------------------------------

# ----------------------------------------
# Graph
# ----------------------------------------
Hour All Visits (Segment) Traffic (Segment) Traffic (Segment)
00:00:00 492 0 492
01:00:00 259 0 259
02:00:00 130 0 130
03:00:00 100 0 100
04:00:00 66 0 66
05:00:00 99 0 99
06:00:00 242 6 236
07:00:00 597 80 517
08:00:00 1,106 268 838
09:00:00 1,421 383 1,038
10:00:00 1,518 438 1,080
11:00:00 1,524 419 1,105
12:00:00 1,436 321 1,115
13:00:00 1,385 243 1,142
14:00:00 1,252 158 1,094
15:00:00 1,299 142 1,157
16:00:00 1,178 87 1,091
17:00:00 1,025 16 1,009
18:00:00 928 7 921
19:00:00 971 3 968
20:00:00 992 3 989
21:00:00 998 3 995
22:00:00 933 0 933
23:00:00 731 0 731
# -------------------------------------------------------------------------------- causing issues in log
;
run;
proc print;
run;
[/pre]

Under SAS 9.0 version , code is OK. Message was edited by: Ksharp
Contributor
Posts: 65

Re: reading in a file from specific rows

When I run this I get 6 variables but no observations.
Super Contributor
Super Contributor
Posts: 3,174

Re: reading in a file from specific rows

Recommend some self-initiated desk-checking using a PUTLOG statement, as shown below -- you might consider placing multiples of these in various points in your program to display variables as the processing continues (increment "nn" value for identification with each being unique":

PUTLOG '>DIAG-nn>' / _all_;


Suggest you share your SAS log back with a post/reply to the forum for additional guidance, if needed further after the desk-checking exercise.

Scott Barry
SBBWorks, Inc.
Ask a Question
Discussion stats
  • 5 replies
  • 136 views
  • 0 likes
  • 5 in conversation