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:
Can some one help.
Thank you in advance.
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.
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.
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.
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!
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.