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: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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