DATA Step, Macro, Functions and more

single quotation between two delimetere causing issue

Reply
Contributor
Posts: 65

single quotation between two delimetere causing issue

Hello,

 

I'm having two single quote (') seprated by delimeter.

its is reading them as 1 and var3 is equal to "8" which should be var4 in second observation. 


data ds1;
infile datalines dsd dlm='|';
input var1-var4;
datalines;
1|2|3|4|
9|'|'|8|
run;

proc print data=ds1;
run;

 

Also I don't want to "~" before variable because in actual case I don't which two consuctive variable will contain single quotation.

 

Actual text which I'm trying to import from unix contains 200 variable and that is tab delimited.. I've given above example of pipe delimited just to more readable. 

 

Please help me out !!

 

Thanks

Atul Deshmukh 

Contributor
Posts: 22

Re: single quotation between two delimetere causing issue

Posted in reply to atul_desh

The behavior you are observing is the purpose of the DSD option as documented. When I removed the DSD option the data read as intended.

Super User
Super User
Posts: 7,042

Re: single quotation between two delimetere causing issue

Posted in reply to atul_desh

So if your input file never has null values then you can just drop the DSD option.

But if your input file has both null values (ie examples where two delimiters are right next to each other and it means there is value that empty) then you will need to process the data line before reading it.

So you could change the |'| into |"'"| so that SAS will see that the single quote is in fact a quoted string and so the DSD option will work.

input @;
do while (index(_infile_,"|'|"));
  _infile_=tranwrd(_infile_,"|'|","|""'""|");
end;
input var1-var5;

Or if you never have actual quoted string values that would also require the DSD option then perhaps you can drop the DSD option from the INFILE and instead convert the || into |.| to indicate a missing value.

input @;
do while (index(_infile_,"||"));
  _infile_=tranwrd(_infile_,"||","|.|");
end;
input var1-var5;
Contributor
Posts: 65

Re: single quotation between two delimetere causing issue

Posted in reply to atul_desh
But my actual file is not pipe delimited it is tab delimeted... Here I've given example of pipe delimited just to get more understand.

Super User
Super User
Posts: 7,042

Re: single quotation between two delimetere causing issue

Posted in reply to atul_desh

atul_desh wrote:
But my actual file is not pipe delimited it is tab delimeted... Here I've given example of pipe delimited just to get more understand.


So in the fix use tabs instead of pipe character.  The hex code for a tab is '09'x.

Ask a Question
Discussion stats
  • 4 replies
  • 114 views
  • 0 likes
  • 3 in conversation