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

I have the text file which is not uniform. I want to read in one variable in continues, which is separated by comma. Like below. Thanks for help.

 
have input:
abc,def,
ghi,jkl,mno
,pqr stuw,xyz

  

want ouput:
abc
def
ghi
jkl
mno
pqr stuw

xyz

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

So if you have the data in a file then read it using RECFM=N and manually remove or replace the end of line characters

You will need to know what end of line characters your file is using.  Normally it is either CR+LF (Windows/DOS) or LF (Unix).

Let's put your example data into a file with CR+LF as the end of line characters.

filename sample temp;
data _null_;
  file sample termstr=CRLF;
  put
 'abc,def'
/'new line,'
/'ghi,jkl,mno'
/',pqr'
/'stuw,xyz'
 ;
run;

Now let's read it in.

data test;
  length word $100 ;
  infile sample dsd recfm=n ;
  input word ;
  if word=:'0D0A'x then word=substr(word,3);
  word = tranwrd(word,'0D0A'x,' ');
  put _n_= word= :$quote.;
run;

Results.

_N_=1 word="abc"
_N_=2 word="def new line"
_N_=3 word="ghi"
_N_=4 word="jkl"
_N_=5 word="mno"
_N_=6 word="pqr stuw"
_N_=7 word="xyz"

View solution in original post

7 REPLIES 7
kiranv_
Rhodochrosite | Level 12

you need to use double trailing @@ 

 

 

 

 data want;
 informat var $20.;
 infile datalines dsd ;
 input var $ @@;
 if var = ' ' then delete;
 datalines;
abc,def,
ghi,jkl,mno
,pqr stuw,xyz
,hhhhhhhhhhhhhhhh,yyy
;
run;

 

Tomtak1234
Fluorite | Level 6

thank you kiranV_. it works..

novinosrin
Tourmaline | Level 20
/*with single trailing*/
data have1;
infile datalines dsd truncover;
input var :$20. @;
do _n_=1 by 1 until(_n_ eq countw(_infile_,',','M'));
if not missing(var) then output;
input var : $20. @;
end;
datalines;
abc,def,
ghi,jkl,mno
,pqr stuw,xyz
;

/*with scan*/
data have2;
infile datalines dsd truncover;
input @;
do _n_=1 to countw(_infile_,',');
var=scan(_infile_,_n_,',');
if not missing(var) then output;
end;
datalines;
abc,def,
ghi,jkl,mno
,pqr stuw,xyz
;
Tomtak1234
Fluorite | Level 6

thank you all dear expert.

I apologies, I miss a condition: the "new line " (enter key) if present I wanted to translate to space (" ") so that it reads in as continues seperated by comma.

for example:

have:

abc,def
new line,
ghi,jkl,mno
,pqr
stuw,xyz

 

want:

abc

def new line
ghi

jkl

mno
pqr stuw

xyz

 

kiranv_
Rhodochrosite | Level 12

this might work.

 data want;
 informat var $20.;
 infile datalines dsd ;
 input var $ @@;
 datalines;
abc,def,
ghi,jkl,mno
,pqr stuw,xyz
,hhhhhhhhhhhhhhhh,yyy
;
run;
Tom
Super User Tom
Super User

So if you have the data in a file then read it using RECFM=N and manually remove or replace the end of line characters

You will need to know what end of line characters your file is using.  Normally it is either CR+LF (Windows/DOS) or LF (Unix).

Let's put your example data into a file with CR+LF as the end of line characters.

filename sample temp;
data _null_;
  file sample termstr=CRLF;
  put
 'abc,def'
/'new line,'
/'ghi,jkl,mno'
/',pqr'
/'stuw,xyz'
 ;
run;

Now let's read it in.

data test;
  length word $100 ;
  infile sample dsd recfm=n ;
  input word ;
  if word=:'0D0A'x then word=substr(word,3);
  word = tranwrd(word,'0D0A'x,' ');
  put _n_= word= :$quote.;
run;

Results.

_N_=1 word="abc"
_N_=2 word="def new line"
_N_=3 word="ghi"
_N_=4 word="jkl"
_N_=5 word="mno"
_N_=6 word="pqr stuw"
_N_=7 word="xyz"
Tomtak1234
Fluorite | Level 6

thank you Tom. I got the expected. and learn many thing from experts.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 7 replies
  • 1725 views
  • 4 likes
  • 4 in conversation