Help using Base SAS procedures

Reading SAS data

Reply
N/A
Posts: 0

Reading SAS data

Hi All,

Many times I get stuck in reading the data files in SAS with variables starting and ending at different positions and I use a long method of making every observation in the data of same length (with length of a variable as maximum length of the observation in that column). If it is required to work on say 1 million of rows then the process becomes almost next to impossible / time consuming.

Suppose I have the sample data:-

2010/06/01 00:00:42 JG Hoover and Candy Web operator1 games 0
2010/06/01 00:04:07 JG sasapplication web operator2 games 0
2010/06/01 00:04:15 JG Games Sport WEB operator2 games 0
2010/06/01 00:35:31 TT BRB Web operator1 music 1

How do I write a code in SAS that will read the above data with variables :

date (2010/06/01 etc...), content_type(00:00:42 etc...), content_category (JG and TT) ,download_channel (Hoover and Candy, sasapplication,Gmes Sport,BRB), Games Sport(Web) , operator (operator1, operator2), service (games,music) , successful_download ( 1 or 0 ).

Kind Regards,
Kritanjli
N/A
Posts: 0

Re: Reading SAS data

Posted in reply to deleted_user
hello,

you should pass from column/formatted input to list input / modified list input, which is far more flexible.


with list input SAS reads a data value until it encounters the delimiter (by default is blank).


anyway with list input default delimiter may cause some troubles if there are variables with embedded blanks (like download_channel in your case), so it may be useful to change the delimiter and then import your data.



my solution is available for your sample data, but it may not be enough to correctly import the entire file.



[pre]

data a;

infile datalines column=x truncover dsd dlm=' ';*no need for truncover dsd dlm=' ' for this sample data;
input date :yymmdd10. content_type :$8. content_category :$2. @;

_infile_=reverse(substr(trim(_infile_),x));

*list;
*put _infile_;

input @1 successful_download service $ operator :$9. Games_Sport $ download_channel & :$35.;


format operator $revers9. service $revers8. Games_Sport $revers8. download_channel $revers35.;

datalines;
2010/06/01 00:00:42 JG Hoover and Candy Web operator1 games 0
2010/06/01 00:04:07 JG sasapplication web operator2 games 0
2010/06/01 00:04:15 JG Games Sport WEB operator2 games 0
2010/06/01 00:35:31 TT BRB Web operator1 music 1
;

[/pre]

Marius
Valued Guide
Posts: 2,177

Re: Reading SAS data

Posted in reply to deleted_user
similar challenge at posting http://support.sas.com/forums/click.jspa?searchID=188615&messageID=49246 is solved by locating the position and length of the column whose length cannot be determined only from a blank delimiter. As here, the call scan() routine can derive the start of the operator column, and the download_channel ends 2 positions before operator starts. The download_channel appears to start in a fixed position (24). So the input operation becomes
data loaded(keep= date time content_category download_channel operator service successful_download ) ;
infile 'your input file' lrecl=1000 truncover ;
input @ ; *to load infile buffer ;
call scan( _infile_, -3, pos, len ) ; * locate POSition of operator column ;
chan_len = pos - 24 -1/* for delimiter*/ ; * derive download_channel length ;
input date yymmdd10. +1 time time8. +1 content_category $2. +1 download_channel $varying50. chan_len
operator :$20. service :$20. successful_download ;
format date yymmdd10. time time8. ;
run ;
Ask a Question
Discussion stats
  • 2 replies
  • 120 views
  • 0 likes
  • 2 in conversation