Index: ccr/trunk/p/CCRMEDS6.m
===================================================================
--- ccr/trunk/p/CCRMEDS6.m	(revision 373)
+++ ccr/trunk/p/CCRMEDS6.m	(revision 374)
@@ -97,28 +97,12 @@
  . ; Field 27 in file 52
  . N NDC S NDC=$$GET1^DIQ(52,RXIEN,27,"I")
- . S NDC=$TR(NDC,"-")  ; Remove dashes
  . ; I discovered that file 176.002 might give you two codes for the NDC
  . ; One for the Clinical Drug, and one for the ingredient.
  . ; So the plan is to get the two RxNorm codes, and then find from
  . ; file 176.001 which one is the Clinical Drug.
- . N C0CZRXN,DIERR
+ . ; ... I refactored this into GETRXN
  . N RXNORM,SRCIEN,RXNNAME,RXNVER
  . I +NDC,$D(^C0CRXN) D  ; $Data is for Systems that don't have our RxNorm file yet.
- . . D FIND^DIC(176.002,,"@;.01","PX",NDC,"*","NDC",,,"C0CZRXN","DIERR")
- . . B:$D(DIERR)
- . . S RXNORM(0)=+C0CZRXN("DILIST",0) ; RxNorm(0) will be # of entries
- . . N I S I=0
- . . F  S I=$O(C0CZRXN("DILIST",I)) Q:I=""  S RXNORM(I)=$P(C0CZRXN("DILIST",I,0),U,2)
- . . ; At this point, RxNorm(0) is # of entries; RxNorm(1...) are the entries
- . . ; If RxNorm(0) is 1, then we only have one entry, and that's it.
- . . I RXNORM(0)=1 S RXNORM=RXNORM(1)
- . . ; Otherwise, we need to find out which is the clinical drug
- . . I RXNORM(0)>1 D
- . . . S I=0
- . . . F  S I=$O(RXNORM(I)) Q:I=""  Q:$L($G(RXNORM))  D
- . . . . N RXNIEN S RXNIEN=$$FIND1^DIC(176.001,,,RXNORM(I),"B")
- . . . . N RXNTTY S RXNTTY=$$GET1^DIQ(176.001,RXNIEN,3)
- . . . . I RXNTTY="CD" S RXNORM=RXNORM(I) QUIT
- . . I $L($G(RXNORM))=0 S RXNORM=""
+ . . S RXNORM=$$GETRXN(NDC)
  . . S SRCIEN=$$FIND1^DIC(176.003,,,"RXNORM","B")
  . . S RXNNAME=$$GET1^DIQ(176.003,SRCIEN,6)
@@ -315,2 +299,26 @@
  Q
  ;
+GETRXN(NDC) ; Extrinsic Function; PUBLIC; NDC to RxNorm
+ ;; Get RxNorm Concept Number for a Given NDC
+ ;
+ S NDC=$TR(NDC,"-")  ; Remove dashes
+ N RXNORM,C0CZRXN,DIERR
+ D FIND^DIC(176.002,,"@;.01","PX",NDC,"*","NDC",,,"C0CZRXN","DIERR")
+ I $D(DIERR) D ^%ZTER BREAK
+ S RXNORM(0)=+C0CZRXN("DILIST",0) ; RxNorm(0) will be # of entries
+ N I S I=0
+ F  S I=$O(C0CZRXN("DILIST",I)) Q:I=""  S RXNORM(I)=$P(C0CZRXN("DILIST",I,0),U,2)
+ ; At this point, RxNorm(0) is # of entries; RxNorm(1...) are the entries
+ ; If RxNorm(0) is 1, then we only have one entry, and that's it.
+ I RXNORM(0)=1 QUIT RXNORM(1)  ; RETURN RXNORM(1)
+ ; Otherwise, we need to find out which one is the semantic
+ ; clinical drug. I built an index on 176.001 (RxNorm Concepts)
+ ; for that purpose.
+ I RXNORM(0)>1 D
+ . S I=0
+ . F  S I=$O(RXNORM(I)) Q:I=""  D  Q:$G(RXNORM)
+ . . N RXNIEN S RXNIEN=$$FIND1^DIC(176.001,,,RXNORM(I),"SCD")
+ . . I +$G(RXNIEN)=0 QUIT  ; try the next entry... 
+ . . E  S RXNORM=RXNORM(I) QUIT  ; We found the right code
+ QUIT +$G(RXNORM)  ; RETURN RXNORM; if we couldn't find a clnical drug, return with 0
+ 
