BookmarkSubscribeRSS Feed
Leonoids
Calcite | Level 5

Hello,

 

I am trying to read the pipe delimited file in which one of the character column is having long description starting from line1 to line3 or line1 to line2.  I am unable to read this in sas dataset. Please help me to resolve the issue.

example:

1009|234|long description in comments

and need to be updated in the sas dataset.

Thank you for uploading data|264,00|345,09|address

1010|235|long description in comments

and need to be updated in the sas dataset|546.08|589|address

 

Thank you in advance.

5 REPLIES 5
MCoopmans
SAS Employee

You could read this with RECFM=N which means 'binary' so the datastep will continue reading across lines untill it reaches a delimiter. 
In your example it does not work fully because there seems to be a delimiter missing at the end of the third row, after "adress". If that line would end with a delimiter this simple datastep would do the trick:

 

data want;
 infile "c:\temp\file.txt" dlm="|" dsd recfm=N;
 input f1 : $10. f2 : $10. comments : $500. ;
run;

with this result:

2020-06-11_100833.png

I had to read all your data as characters since the numbers are not using decimal comma and dots consistently. Is that voluntary ?

Leonoids
Calcite | Level 5

There is no delimiter at the end of the record. 

Ksharp
Super User
options compress=yes;
data have;
infile cards length=len;
input temp $varying2000. len;
cards;
1009|234|long description in comments
and need to be updated in the sas dataset.
Thank you for uploading data|264,00|345,09|address
1010|235|long description in comments
and need to be updated in the sas dataset|546.08|589|address
;

data temp;
 set have;
 length x $ 8000;
 retain x;
 x=cats(x,temp);
 if countc(x,'|')=5 then do;output;call missing(x);end;
 drop temp;
run;
data want;
 set temp;
 a1=scan(x,1,'|');
 a2=scan(x,2,'|');
 a3=scan(x,3,'|');
 a4=scan(x,4,'|');
 a5=scan(x,5,'|');
 a6=scan(x,6,'|');
 drop x;
run;
Ksharp
Super User

Here is an alternative way.

 

options compress=yes;
data have;
infile cards length=len;
input temp $varying2000. len;
if prxmatch('/^\d+\|\d+/',temp) then group+1;
cards;
1009|234|long description in comments
and need to be updated in the sas dataset.
Thank you for uploading data|264,00|345,09|address
1010|235|long description in comments
and need to be updated in the sas dataset|546.08|589|address
;

data temp;
length x $ 8000;
do until(last.group);
 set have;
 by group;
 x=cats(x,temp);
end;
drop temp;
run;
data want;
 set temp;
 a1=scan(x,1,'|');
 a2=scan(x,2,'|');
 a3=scan(x,3,'|');
 a4=scan(x,4,'|');
 a5=scan(x,5,'|');
 a6=scan(x,6,'|');
 drop x;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1353 views
  • 0 likes
  • 4 in conversation