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,
Hi
You can combine the @"name" pointer control with Modified List Input styles to read this kind of data, see example below:
Bruno
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,"=");
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
Hi
You can combine the @"name" pointer control with Modified List Input styles to read this kind of data, see example below:
Bruno
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.
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
Thanks Bruno. And thanks to all for your help.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.