DATA Step, Macro, Functions and more

Read Value with inverted comma

Accepted Solution Solved
Reply
Contributor
Posts: 60
Accepted Solution

Read Value with inverted comma

I've want to read var2 & var8 with (") in it.

 

I think dsd considering inverted comma as delimeter. 

 

how can I read value ? please help !!

 

data ds2;
infile datalines dsd dlm="|" missover;
input (var1-var8) ($);
datalines;
2|"28 |4|||||29" |
run;

proc print data=ds2;
run;


Accepted Solutions
Solution
‎06-28-2017 08:11 AM
PROC Star
Posts: 7,363

Re: Read Value with inverted comma

My original code had an uncorrected oversight which I'll correct after posting this. The following will work with your tab delimited file:

data ds2;
  infile datalines dlm='09'x missover;
  input @;
  do while (index(_infile_,'0909'x) gt 0);
    _infile_=tranwrd(_infile_,'0909'x,'092E09'x);
  end;
  input (var1-var8) ($);
  datalines;
2	"28 	4					29"	
;
run;

Art, CEO, AnalystFinder.com

View solution in original post


All Replies
Super User
Posts: 9,676

Re: Read Value with inverted comma

data ds2;
infile datalines ;
input;
var1=scan(_infile_,1,'|','m');
var2=scan(_infile_,2,'|','m');
var3=scan(_infile_,3,'|','m');
var4=scan(_infile_,4,'|','m');
var5=scan(_infile_,5,'|','m');
var6=scan(_infile_,6,'|','m');
var7=scan(_infile_,7,'|','m');
var8=scan(_infile_,8,'|','m');

datalines;
2|"28 |4|||||29" |
run;

proc print data=ds2;
run;
Super User
Posts: 10,497

Re: Read Value with inverted comma

If your data does not contain any Pipe characters as part of the actual data then you probably do not need DSD to read it.

If you data is not pipe delimited then you should indicate how the data is actually structured.

 

 

Super User
Posts: 6,936

Re: Read Value with inverted comma

@ballardw the problem comes from the dual function of dsd; one needs dsd so that two or more successive delimiters are read as missing values. But then SAS also drops the quotes.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 60

Re: Read Value with inverted comma

yup.. is there any way so that dsd don't consider double quotes & single quotes as delimiter and only consider Pipe or '09'x ??
Contributor
Posts: 60

Re: Read Value with inverted comma

My data is in tab delimited text file... but I shown here as pipe delimited just to more readable.
PROC Star
Posts: 7,363

Re: Read Value with inverted comma

The following should work:

data ds2;
  infile datalines dsd dlm="|" missover;
  input @;
  _infile_=translate(_infile_,'~','"');
  input (var1-var8) ($);
  array have(*) $ var1-var8;
  do i=1 to dim(have);
    have(i)=translate(have(i),'"','~');
  end;
datalines;
2|"28 |4|||||29" |
run;

Art, CEO, AnalystFinder.com

Contributor
Posts: 60

Re: Read Value with inverted comma

thank you but Actual data which I'm having is having 180 variable and 85 million observation.. so using array make them more difficult to run...longer to run.
PROC Star
Posts: 7,363

Re: Read Value with inverted comma

Do you need to keep the quotation marks? If not, the array isn't needed as they could just be compressed rather than converted to a tilde.

 

Art, CEO, AnalystFinder.com

Contributor
Posts: 60

Re: Read Value with inverted comma

Yes I do need quotation mark !! Smiley Sad
PROC Star
Posts: 7,363

Re: Read Value with inverted comma

[ Edited ]

Then you could use:

data ds2;
infile datalines dlm="|" missover;
input @;
do while (index(_infile_,'||') gt 0);
_infile_=tranwrd(_infile_,"||","|.|");
end;
input (var1-var8) ($);
datalines;
2|"28 |4|||||29" |
;
run;

Art, CEO, AnalystFinder.com

Contributor
Posts: 60

Re: Read Value with inverted comma

In Actaul file it is tab delimeted... I'm trying to replace | with '09'x..
Solution
‎06-28-2017 08:11 AM
PROC Star
Posts: 7,363

Re: Read Value with inverted comma

My original code had an uncorrected oversight which I'll correct after posting this. The following will work with your tab delimited file:

data ds2;
  infile datalines dlm='09'x missover;
  input @;
  do while (index(_infile_,'0909'x) gt 0);
    _infile_=tranwrd(_infile_,'0909'x,'092E09'x);
  end;
  input (var1-var8) ($);
  datalines;
2	"28 	4					29"	
;
run;

Art, CEO, AnalystFinder.com

Contributor
Posts: 60

Re: Read Value with inverted comma

@art297 I'm getting issue when my first column is blank...even though i've tried missover, truncover but issue still persist. 

Super User
Super User
Posts: 6,499

Re: Read Value with inverted comma


atul_desh wrote:

@art297 I'm getting issue when my first column is blank...even though i've tried missover, truncover but issue still persist. 


Add code to handle that case.

if _infile_=: '09'x then _infile_='.'||_infile_;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 16 replies
  • 257 views
  • 3 likes
  • 6 in conversation