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

 

 

 

 

 Hello SAS users,

 

I have a question on how to get this right. I have a 10 digit NDC code in 3 formats as shown below. I need to add a '0' in specified places based on the format. Below is my code to solve this but I keep getting errors with the use of substring. I do know I can use scan to complete the process. But, I want to know if there is an efficient way to do this.

 

 

NDC packagecode is same as NDC code.

 

seq1endpos = find ( ndcpackagecode , "-") ;
seg1 = substr ( ndcpackagecode , 1 , seq1endpos - 1 ) ;
seq2endpos = find ( substr ( ndcpackagecode , seq1endpos + 1 , 10 ) , "-") ;
seg2 = substr ( ndcpackagecode , seq1endpos + 1 , seq2endpos - 1 ) ;
seg3 = substr ( ndcpackagecode , seq1endpos + seq2endpos + 1 , 2 ) ;
compressedcode = compress ( ndcpackagecode , "-" , "") ;
if length(trim(seg1)) = 5 then do ; 
if length(trim(seg2)) = 4 and length(trim(seg3)) = 2 then do ;
ndccode = trim(seg1) || trim(seg2) || trim(seg3) ; ndcmod = 0 ; 
end ; 
if length(trim(seg2)) = 4 and length(trim(seg3)) = 1 then do ;
ndccode = trim(seg1) || trim(seg2) || '0' || trim(seg3) ; ndcmod = 1 ; 
end ; 
if length(trim(seg2)) = 3 and length(trim(seg3)) = 2 then do ;
ndccode = trim(seg1) || '0' || trim(seg2) || trim(seg3) ; ndcmod = 1 ; 
end ; 
if length(trim(seg2)) = 3 and length(trim(seg3)) = 1 then do ;
ndccode = trim(seg1) || '0' || trim(seg2) || '0' || trim(seg3) ; ndcmod = 1 ; 
end ;
end ;
if length(trim(seg1)) = 4 then do ; 
if length(trim(seg2)) = 4 and length(trim(seg3)) = 2 then do ;
ndccode = '0' || trim(seg1) || trim(seg2) || trim(seg3) ; ndcmod = 1 ; 
end ; 
if length(trim(seg2)) = 4 and length(trim(seg3)) = 1 then do ;
ndccode = '0' || trim(seg1) || trim(seg2) || '0' || trim(seg3) ; ndcmod = 1 ; 
end ;
end ;

if length(ndccode) ne 11 then ndcbadlength = 1 ; else ndcbadlength = 0 ;

 

NDC Conversion: 10-digit to 11-digit

 

If 10-digit NDC format is:

Then add a zero (0) in:

Report NDC as:

4-4-2

9999-9999-99

1st position 09999-9999-99

09999999999

5-3-2

99999-999-99

6th position 99999-0999-99

99999099999

5-4-1

99999-9999-9

10th position 99999-9999-09

99999999909

 

Here is another example of how the end result should be: 

Capture.PNG

Can some one help.

Thank you in advance.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Robot surprised

 

To calculate the different segments, Use SCAN() to separate your components. 

Convert them to numerics using INPUT.

Use Zw to apply the format and recreate your new variable. 

Use CATX() to recreate your new variable.

 

 

The required structure is 5-4-2? Note that you never actually say that anywhere. If it's not, you can change the code.

 

Also, please post sample data as text, preferable in a data step but at least text. Otherwise it's hard to work with your data and it takes longer for you to get an answer.

 

delete_ndc.JPG

 

 

 

 

View solution in original post

2 REPLIES 2
Reeza
Super User

Robot surprised

 

To calculate the different segments, Use SCAN() to separate your components. 

Convert them to numerics using INPUT.

Use Zw to apply the format and recreate your new variable. 

Use CATX() to recreate your new variable.

 

 

The required structure is 5-4-2? Note that you never actually say that anywhere. If it's not, you can change the code.

 

Also, please post sample data as text, preferable in a data step but at least text. Otherwise it's hard to work with your data and it takes longer for you to get an answer.

 

delete_ndc.JPG

 

 

 

 

shasank
Quartz | Level 8

Hi Reeza,

 

Thank you for your solution.

Sorry about the text issue.

 

The data posted was in 2 forms one as a table and another as a image. I posted the Image due to the formatting issue when as a text.

 

Thank you for the help. your solution worked.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 3823 views
  • 0 likes
  • 2 in conversation