BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Spacetime
Obsidian | Level 7


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.

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

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

6 REPLIES 6
shivas
Pyrite | Level 9

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

Spacetime
Obsidian | Level 7

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

data_null__
Jade | Level 19

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

Spacetime
Obsidian | Level 7

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

Astounding
PROC Star

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.

Spacetime
Obsidian | Level 7

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 6 replies
  • 704 views
  • 3 likes
  • 4 in conversation