Help with input statement

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

Help with input statement


Hi,

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

CLCODE;AGENT

*;50002

*;50002

;50002

*;50002

*;50002

Call it test.txt.

I try the input step -

data test;

infile test DLM=';' FIRSTOBS=2 LRECL=350 MISSOVER;

input

CLCODE $1.

AGENT $5.;

run;

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.


Accepted Solutions
Solution
‎06-13-2012 03:40 PM
Respected Advisor
Posts: 3,777

Re: Help with input statement

Formatted input does not work with DSD DLM a form of list input.

input

CLCODE $1.

AGENT $5.;

Should use the : format modifier.

input

CLCODE : $1.

AGENT : $5.;

View solution in original post


All Replies
Super Contributor
Posts: 349

Re: Help with input statement

Hi,

Try this..Hope it helps..

data test;

infile  'F:\Community_SAS\funds.txt' dsd dlm= ';' FIRSTOBS=2 LRECL=350 ;

length clcode $1.;

length agent 8.;

input

CLCODE $

AGENT ;

run;

Thanks,

Shiva

Occasional Contributor
Posts: 14

Re: Help with input statement

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).

Solution
‎06-13-2012 03:40 PM
Respected Advisor
Posts: 3,777

Re: Help with input statement

Formatted input does not work with DSD DLM a form of list input.

input

CLCODE $1.

AGENT $5.;

Should use the : format modifier.

input

CLCODE : $1.

AGENT : $5.;

Occasional Contributor
Posts: 14

Re: Help with input statement

Thank you very much, that does work.  That was the link I was missing.

Super User
Posts: 5,099

Re: Help with input statement

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:

data want;

infile test dlm=';' firstobs=2 dsd lrecl=350;

length clcode $ 1 agent $ 5;

input @;

if _infile_ =: ';'  then input agent;

else input clcode agent;

run;

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.

Good luck.

Occasional Contributor
Posts: 14

Re: Help with input statement

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

input

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.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 308 views
  • 3 likes
  • 4 in conversation