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

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,

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

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

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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,"=");

MikeZdeb
Rhodochrosite | Level 12

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

BrunoMueller
SAS Super FREQ

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

MikeZdeb
Rhodochrosite | Level 12

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.

Ksharp
Super User

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

BrunoMueller
SAS Super FREQ

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

cxkev
Fluorite | Level 6

Thanks Bruno. And thanks to all for your 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
  • 7 replies
  • 3728 views
  • 0 likes
  • 5 in conversation