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

Hello SAS guys,

I have data like below, the want data did not  give me  second response empty of id 123 and sixth response empty of id 456. How do I get the right answer? Thanks a lot!

 

data have ;
length id $3 response $15 ;
input id response ;
datalines ;
123 A||B|C|D|A|C
456 A|A|C|D|B||C
;
run ;

data want;
set have;
cnt=countw(response,'|');
i=1;
do while (i<cnt);
resp=scan(response,i,'|');
output;
i=i+1;
end;
drop response;
rename resp=response;
run;

 

the output is 

123 A

123 B

123 C

123 D

123 A

456 A

456 A

456 C

456 D

456 B

But I need :

123  A

123    

123 B

123 C

.....

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Normally (as you can see), SCAN treats consecutive delimiters as one large delimiter.  If you want them each to mark a separate word, you can tell SCAN to do that:

 

resp=scan(response,i,'|', 'M') ;

View solution in original post

4 REPLIES 4
Astounding
PROC Star

Normally (as you can see), SCAN treats consecutive delimiters as one large delimiter.  If you want them each to mark a separate word, you can tell SCAN to do that:

 

resp=scan(response,i,'|', 'M') ;

daisy6
Quartz | Level 8

Even I put resp=scan(response,i,'|', 'M') ;

the results are

id     cnt    i     response

123  6       1   A

123 6        2   

123 6       3   B

123 6        4  C

123 6       5    D

456 6       1  A

456  6      2  A

456  6      3  C

456  6      4  D

456   6      5  B

it is not the result which match with the original data

 

daisy6
Quartz | Level 8

Thank you very much Astounding. I adjusted my code and your help works

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Pretty sure I covered this in your other post, but:

data have;
  length id $3 response $15;
  input id response;
datalines;
123 A||B|C|D|A|C
456 A|A|C|D|B||C
;
run;

data want (drop=i);
  set have;
  do i=1 to lengthn(response);
    if char(response,i) ne "|" then result=char(response,i);
    else do;
      output;
      result="";
    end;
  end;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 2906 views
  • 2 likes
  • 3 in conversation