BookmarkSubscribeRSS Feed
neilxu
Calcite | Level 5
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
5 REPLIES 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
Ksharp
Super User
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
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
INPUT logic in Ksharp reply supports a max of 1 blank-space between words in the comments.

Scott Barry
SBBWorks, Inc.
Ksharp
Super User
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. 🙂


Ksharp Message was edited by: Ksharp
Peter_C
Rhodochrosite | Level 12
neilxu

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

peterC

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

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3284 views
  • 0 likes
  • 4 in conversation