Help using Base SAS procedures

Using Input in SAS

Reply
N/A
Posts: 0

Using Input in SAS

Hi All,

I have the following data and the variables are: Institution, Rate and Years. How do I run the data. Is there a way that I can run the data with just knowledge of the location of the file say at - "C:\CWA\bank.txt"; and retrieve the data in improper form?

I guess that length statements have to be used here.

"bank.txt" data:-

MBNA America 0.0817 5
Metro Bank 0.0814 3
Standard Bank 0.0806 4
XYZ 0.0732 5
Australian National Bank 0.043 3


Kind Regards,
Kriti
Super Contributor
Super Contributor
Posts: 3,174

Re: Using Input in SAS

Posted in reply to deleted_user
If I understand correctly, you want to "input the data" from an external file (you list) using SAS. You have a few options, depending on what control you want to have, as follows for your review/decision:

1) PROC IMPORT
2) DATA step programming logic using an INFILE and INPUT statements.

And, with option #1 above, SAS will generate the DATA step for you based on the external input data-field content.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search arguments, this topic / post:

proc import site:sas.com

proc import txt file site:sas.com

input external data site:sas.com

data step programming site:sas.com
N/A
Posts: 0

Re: Using Input in SAS

Posted in reply to deleted_user
Hello,

If you want to read your data with a data step a good solution may be the
use of reverse format since your variable, institution, contains embedded blanks (you can
see this posting, it may help: http://support.sas.com/forums/thread.jspa?messageID=47925묵 )
:

data s (drop=rate_int Institution_int);
infile "C:\CWA\bank.txt" ;
input @;
_infile_=reverse(_infile_);
input Years Rate_int $ Institution_int & $30. ;
Rate=input(put(Rate_int, $revers8.),best12.);
Institution=put(Institution_int,$revers25.);
run;

Marius
Super Contributor
Super Contributor
Posts: 3,174

Re: Using Input in SAS

Posted in reply to deleted_user
Marius makes a very observant point that I overlooked. The OP has input data with a "blank delimiter", however the first data-field also "appears" to have embedded blanks, creating an "input logic" challenge. Given the prior post from Marius, I would submit that PROC IMPORT will not work.

Also, with an HTML-formatted post for the OP, it's really impossible to interpret just how the external data file is formatted, either with "two or more blanks" separating each field, or a non-printable delimiter character, or ideally a fixed-format (column-controlled) data-file layout.

To the question posed, if the input data is a fixed-format layout, where input fields occupy specific a column (or column range), the INPUT statement and the INFORMAT specification will be sufficient, such as $CHAR15. yields a SAS CHARACTER variable, length 15.

Scott Barry
SBBWorks, Inc.

Additional suggested Google advanced search argument, this topic / post:

data step programming input statement external file site:sas.com
Valued Guide
Posts: 2,177

Re: Using Input in SAS

awkward for a data step, but not impossible because the challenge - an unquoted column which might hold an embedded delimiter - occurs in one fixed (data) column - in this case it is the first column.
A presentation entitled "more _infile_ magic" http://www2.sas.com/proceedings/sugi28/086-28.pdf draws attention to the idea of updating the _infile_ buffer before using input statements.
Here we might want to quote the first data column. We can define the end of that by the beginning of the second last column. Although the SCAN(_infile_,-2) function does provide the text of the second last column, more help comes from the CALL routine:[pre]call scan( _infile_, -2, POS, LEN ) ; [/pre]provides the all important POS =position of the RATE column.
However, rather than shift the _infile_ buffer and insert quotes, we can use another piece of the amazingly diverse parsing of the INPUT statement, - $varying informat http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#a000193602.htm after querying the buffer to discover the length of that first data column.

So assuming the physical file name is provided in parameter &Minfile, this solves the problem.[pre]%let minfile = C:\CWA\bank.txt ;

filename minfile "&minfile" lrecl=1000 ;

data got_it( keep= institution Rate Years ) ;
infile Minfile ;
input@ ; * load infile buffer *;
* now get position of second-last column *;
call scan( _infile_, -2, POS, LEN, ' ' ) ;
* that defines the length of this Institution *;
inst_len= pos-1 ;
* so use $varying to read that ill-defined column *;
input institution $varying50. inst_len Rate Years ;
run ;[/pre] of course this solution depends on the Rate and Years data being present.
Ask a Question
Discussion stats
  • 4 replies
  • 424 views
  • 0 likes
  • 3 in conversation