Help using Base SAS procedures

How to import text with space delimiter and space is valid in the field?

Reply
Contributor
Posts: 33

How to import text with space delimiter and space is valid in the field?

I have the following text:

id1 string1 comment string2
001 001 qwer asd 5
002 002 asdfyuiop 6
003 003 zxcv 5

The problem is that space is the delimiter and also is the valid in comments. How can I import these?

Thanks
Super Contributor
Super Contributor
Posts: 3,174

Re: How to import text with space delimiter and space is valid in the field?

You will need to use a DATA step and parse the first, second fields, and then after finding the offset character to the third field using INDEXC possibly, then use SUBSTR along with the SAS special variable _INFILE_ to assign the final variable.

Example:

DATA ;
INFILE /* additional parameters as needed */ ;
* LOAD THE INPUT BUFFER. ;
INPUT ;
* LENGTH OR ATTRIB statements go here. ;
F1 = SCAN(_INFILE_,1," ");
F2 = SCAN(_INFILE_,2," ");
OFFSET = INDEXC(_INFILE_," ");
OFFSET = INDEXC(SUBSTR(_INFILE_,OFFSET+1)," ");
F3 = SUBSTR(_INFILE_,OFFSET+1);
PUTLOG _ALL_;
RUN;

Scott Barry
SBBWorks, Inc.
Super User
Posts: 9,671

Re: How to import text with space delimiter and space is valid in the field?

Hi.
You can input 'comment' and 'string2' as one variable firstly, then parse this variable into two variables you need. try it.
[pre]
data temp;
input d1 $ string1 $ comment & $50.;
_comment=substr(comment,1,length(comment)-2);
string2=substr(comment,length(comment)-1);
drop comment;
datalines;
001 001 qwer asd 5
002 002 asdfyuiop 6
003 003 zxcv 5
;
run;

proc print noobs;
run;

[/pre]



Ksharp
Super Contributor
Super Contributor
Posts: 3,174

Re: How to import text with space delimiter and space is valid in the field?

INPUT logic in Ksharp reply supports a max of 1 blank-space between words in the comments.

Scott Barry
SBBWorks, Inc.
Super User
Posts: 9,671

Re: How to import text with space delimiter and space is valid in the field?

Emmm.Yes.
If you have more than two blanks, you can use formatted input method.


[pre]
data temp;
infile datalines truncover;
input d1 $ string1 $ comment $50.;
_comment=substr(comment,1,anydigit(comment)-1);
string2=scan(comment,-1);
drop comment;
datalines;
001 001 qwer asd 5
002 002 asdfyuiop 6
003 003 zxcv sdit 5
;
run;

proc print noobs;
run;

[/pre]


P.S : The company is the one I worked for , not mine. Smiley Happy


Ksharp Message was edited by: Ksharp
Valued Guide
Posts: 2,174

Re: How to import text with space delimiter and space is valid in the field?

neilxu

what are the rules that define the end of "comment"?

peterC
Ask a Question
Discussion stats
  • 5 replies
  • 317 views
  • 0 likes
  • 4 in conversation