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

Hi,

 

I have a data set where I have two codes in the same column:

 

SAS.PNG

 

I would like to split the two codes in two distinct lines, having into acount that the other attributes don't change. For example:

 

SRV_ERROR_CODE  DESC_ERRO_SERVIC  SMI_ERROR_CODE  ERROR_CODE_SMI  SMI_STATE   SRV

50070                           Subprocesso O...            30089                          30089                          PRBL             TER

50070                           Subprocesso O...            30089                          30089                          PRBL             TER

 

Is there any SAS function that doses this? 

Sometimes I have two codes to split, sometimes 3 codes, sometimes 4... I don't have any exact rule...

 

Thank you!

Rita Viegas

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
COUNTW() to count the number of items
SCAN() to split.
temp = smi_error_code;
do i=1 to countw(temp, '|');
smi_error_code = scan(temp, i, '|');
output;
end;

View solution in original post

3 REPLIES 3
Reeza
Super User
COUNTW() to count the number of items
SCAN() to split.
temp = smi_error_code;
do i=1 to countw(temp, '|');
smi_error_code = scan(temp, i, '|');
output;
end;
ballardw
Super User

Code or task?

Are all of the codes 5 characters or less?

 

Assuming a single code in that field appears as |30089| then this code should work. I have no ideas for task approaches:

data want;  
   set have;
   /* the length below needs to be at least as large as
      your current code variable; 48 should work for up to
      8 code values
   */
   length oldcode $ 48.;
   retain oldcode;
   oldcode = code;
   do i=1 to ( countw(oldcode,'|') -1);
      code = scan(oldcode,i,'|');
      output;
   end;
   drop oldcode i;
run;

The loop counter has -1 because your last delimiter | actually does not appear to have a value after it, so the count returned by COUNTW is one too large.

 

pink_poodle
Barite | Level 11

This would split the numbers:

 

...
* For a code segment of 5 characters;
do i = 2 to length( SMI_ERROR_CODE ) by 6;
SMI_ERROR_CODE_new = substr( SMI_ERROR_CODE, i, 4 )
output; end;

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1154 views
  • 0 likes
  • 4 in conversation