DATA Step, Macro, Functions and more

Import .lst file

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

Import .lst file

Hi,

I need to import a .lst text file which is in the following format:

Field1=Value|Field2=Value|Field3=Value

Field1=Value|Field2=Value|Field3=Value

Field1=Value|Field2=Value|Field3=Value

Field1=Value|Field2=Value|Field3=Value

Is there any way to get SAS to realise the "Fieldx=" part is actually the column name and not part of the data?

Thanks,


Accepted Solutions
Solution
‎08-11-2015 12:49 PM
SAS Super FREQ
Posts: 682

Re: Import .lst file

Hi

You can combine the @"name" pointer control with Modified List Input styles to read this kind of data, see example below:

data x;
  infile cards dlm="|" ;
  input
    @
"Field1=" field1 : 8.
    @
"Field2=" field2 : 8.
    @
"Field3=" field3 : $16.
  ;
  datalines;
Field1=5|Field2=10|Field3=Mike
Field1=6|Field2=11|Field3=Armondo
Field1=7|Field2=12|Field3=Hillary
Field1=8|Field2=13|Field3=Barack
;

Bruno

View solution in original post


All Replies
Super User
Super User
Posts: 7,392

Re: Import .lst file

Well, not seen named input with a delimiter before, any reason why the two different types of file format are mixed?  To read normal named variable data you would do:

data want;

  input field1=$ field2=$ field3=$;

datalines;

Field1=Value Field2=Value Field3=Value

Field1=Value Field2=Value Field3=Value

;

run;

To get your file to drop | you could do:

data _null_;

     infile "xyz.txt" recfm=n;

     file "xyz.txt" recfm=n;

     input a $char1.;

     if a="|" then a=" ";

     put a;

run;

Or you could just read the file in with | as delimiter, and then for each column set value as scan(value,2,"=");

Valued Guide
Posts: 765

Re: Import .lst file

Hi.  Tried NAMED INPUT.  It seems that NAMED INPUT is fussy about a space delimiter, so you can change the bar to a space in the data step as you read the data.

data x;

input @;

_infile_ = translate(_infile_,' ','|');

input field1= field2= field3=$10.;

datalines;

Field1=5|Field2=10|Field3=Mike

Field1=6|Field2=11|Field3=Armondo

Field1=7|Field2=12|Field3=Hillary

Field1=8|Field2=13|Field3=Barack

;

DATA SET X ...

Obs    field1    field2    field3

1        5        10      Mike

2        6        11      Armondo

3        7        12      Hillary

4        8        13      Barack

Solution
‎08-11-2015 12:49 PM
SAS Super FREQ
Posts: 682

Re: Import .lst file

Hi

You can combine the @"name" pointer control with Modified List Input styles to read this kind of data, see example below:

data x;
  infile cards dlm="|" ;
  input
    @
"Field1=" field1 : 8.
    @
"Field2=" field2 : 8.
    @
"Field3=" field3 : $16.
  ;
  datalines;
Field1=5|Field2=10|Field3=Mike
Field1=6|Field2=11|Field3=Armondo
Field1=7|Field2=12|Field3=Hillary
Field1=8|Field2=13|Field3=Barack
;

Bruno

Valued Guide
Posts: 765

Re: Import .lst file

Thanks ... having never used named input (who keeps data in that format), didn't know about using it in combination with the LIST input informats.

It might be helpful to add that little tidbit to the on-line help when one looks at the description of NAMED INPUT.  There are a four examples none of which contain a delimiter other than a space.

Super User
Posts: 9,671

Re: Import .lst file

Mike,

There is a problem for Bruno's code.

If there is a row missing Field3= Like

Field1=5|Field2=10


Bruno'code will lost cards and throw an error. But the Named input method will retain its right.

Why Named input method can't handle dlm=',' option, it seems take blank as its delimiter. It is disappointed.


Xia Keshan

SAS Super FREQ
Posts: 682

Re: Import .lst file

Hi Xia

Yes the named input only takes blanks as a delimiter, would be great if we could use the DLM= option to specify another delimiter. So the code from will work better if one has missing data.

Bruno

Contributor
Posts: 28

Re: Import .lst file

Thanks Bruno. And thanks to all for your help.

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 511 views
  • 0 likes
  • 5 in conversation