06-13-2012 12:33 PM
In my new role, I need to do a lot of inputting flat files pulled from the mainframe. I am having a hard time writing input statements for these, that work in a consistent way. Clearly I am not fully understanding how to do this.
For example, if I have data in a .txt file that looks like
Call it test.txt.
I try the input step -
infile test DLM=';' FIRSTOBS=2 LRECL=350 MISSOVER;
I do not get the expected result. It ignores the delimiter, and instead treats it as part of the string. So when I have a *, I wind up with clcode = * and agent = ;5000. When I do not have a *, I wind up with clcode = ; and agent = 50002.
Why will it not work to simply use the DLM as the separator and import each field in the designated informat?
Even excel can very quickly and correctly open the test.txt file, with only knowing the delimiter. SAS knows the delimiter and more information and cannot seem to import it correctly.
Thanks for any help, I'm sure I'm missing something basic, but as I said, I have not done a lot of importing flat files in my past life.
06-13-2012 12:57 PM
Try this..Hope it helps..
infile 'F:\Community_SAS\funds.txt' dsd dlm= ';' FIRSTOBS=2 LRECL=350 ;
length clcode $1.;
length agent 8.;
06-13-2012 03:22 PM
Thanks for the response! Your idea did help to avoid getting the delimiter, although I still get a strange result in the case where the CLCODE is missing. I get the sense it may be looking for that length of $1., even in the case where the value is missing. I'd like it to work by just checking before the delimiter, and if it's missing just taking a missing value. In this case, when the clcode above is missing, it inputs the first character of the Agent number (which is actually a character).
06-13-2012 04:09 PM
Correct me if I'm wrong, but all the solutions so far run into trouble with the line of data that begins with a delimiter. SAS ignores the leading delimiter every time. Fixing the problem is a little harder:
infile test dlm=';' firstobs=2 dsd lrecl=350;
length clcode $ 1 agent $ 5;
if _infile_ =: ';' then input agent;
else input clcode agent;
The program examines the first character of the incoming data line to see whether or not it begins with a delimiter. It inputs either both variables, or just one, depending on what it finds in that first character.
06-13-2012 04:19 PM
Thanks, it seems like there are a couple ways to go about it. I have learned a lot about how input works from reading these responses. I made 3 changes to my original code to get it to work perfectly. 2 were to add DSD and TRUNCOVER options (did not have these before). From what I gather the DSD is what makes it recognize my delimiter. The 3rd was to add the colons before the informats
CLCODE : $1.
AGENT : $5.;
From what I gather, in effect, this makes SAS do the same sort of check your code is performing. It only pays attention to the informat so long as it has not hit the delimiter. So if there is a blank, once it hits the delimiter it moves on, rather than inputting the delimiter.
I think your example would be a way to get it to work too though.
Thanks again to all for the help.
Need further help from the community? Please ask a new question.