BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
2 REPLIES 2
deleted_user
Not applicable
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
Peter_C
Rhodochrosite | Level 12
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 ;

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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