YSLXDG ; ALB/RBD - ICD-10 DIAGNOSIS CODE LOOK UP FOR MENTAL HEALTH ;10 May 2013  11:17 AM
 ;;5.01;MENTAL HEALTH;**107**;Dec 30, 1994;Build 23
 ;
 ;based on ^ZZLXDG which is the standard Diagnosis Search Protocol
 ;beginning routine.
 ;
 Q
 ;
EN ;
 D INITVARS ;set standards variables, you might not need this if it
 ; was already done in your application
 N YSQUIT ; to manage loop
 K YSRETV ;to store the selected code information
 N YSPARAM ;  to set your application specific prompts and messages
 N YSCSYS ;coding system "ICD9" or ICD10"
 N YSOUT ;to return all available information about the selected code
 ;settings:
 D SETPARAM(.YSPARAM) ;edit the SETPARAM subroutine below to set your
 ; application specific prompts
 I YSDT'>0 S YSRETV=-1 Q
 ;starting main loop
 S YSQUIT=0 F  Q:YSQUIT=1  D
 . S YSRETV=0,YSOUT=""
 . W !!   ;reprompt a few lines down
 . ;prompt for the date of interest (date should be available for MH)
 . I YSDT'>0 S YSRETV=-1,YSQUIT=1 Q
 . ;S YSDT=$$ASKDATE(YSPARAM("ASKDATE"))
 . ;prompt for "try again" with "No" as default if ^ or null entered
 . ;for the date or if timed out
 . I YSDT'>0 S:$$QUESTION(2,YSPARAM("TRY ANOTHER"))'=1 YSQUIT=1 Q
 . ;determine coding system based on the date of interest
 . ;If coding system not ICD-10 or greater, then Quit (let MH code
 . ; handle it as before for now)
 . S YSCSYS=$$ICDSYSDG(YSDT) I YSCSYS=1 S YSRETV=-1,YSQUIT=1 Q
 . ;set default response for your prompt
 . S YSDFLT=""
 . ;run either ICD9 or ICD10 prompt/search/select logic
 . ;ICD9 (1 is a pointer to the ICD-9 diagnosis system entry in the
 . ;new file #80.4)
 . I YSCSYS=1 S YSRETV=$$DIAG9(YSDT,YSDFLT,.YSOUT,.YSPARAM) I YSRETV=-2 S:$$QUESTION(1,YSPARAM("TRY ANOTHER"))'=1 YSQUIT=1 Q
 . ;ICD10 (30 is a pointer to the ICD-10 diagnosis system entry in the new file #80.4)
 . I YSCSYS=30 S YSRETV=$$DIAG10(YSDT,YSDFLT,.YSPARAM)
 . I $P(YSRETV,U,2)="LIST CHOICE" S YSRETV=$P(YSRETV,U,1),YSQUIT=1 Q
 . ;display information about the code selected
 . I YSRETV>0 W !,"SELECTED: " D CODEINFO(YSRETV) S YSQUIT=1 Q
 . ;if no data found
 . I YSRETV="" W !!,YSPARAM("NO DATA FOUND") S:$$QUESTION(1,YSPARAM("TRY ANOTHER"))'=1 YSQUIT=1,YSRETV=-1 Q
 . ;in ICD10 if the user answered NO for the question "Do you wish to continue(Y/N)?"
 . I YSRETV=-4 S:$$QUESTION(1,YSPARAM("TRY ANOTHER"))'=1 YSQUIT=1 Q
 . ;no data or was aborted
 . I YSRETV=-2 S:$$QUESTION(1,YSPARAM("TRY ANOTHER"))'=1 YSQUIT=1 Q
 . ;if exit due to ^ in the ICD Diagnosis code prompt
 . I YSRETV=-3 S:$$QUESTION(2,YSPARAM("TRY ANOTHER"))'=1 YSQUIT=1 Q
 . ;if no data found
 . I YSRETV=-1 S:$$QUESTION(2,YSPARAM("TRY ANOTHER"))'=1 YSQUIT=1 Q
 . ; if continue search
 Q
 ;
 ;//---------
 ;The entry point for ICD-10 diagnosis search functionality
 ;can be called from applications directly
 ;input parameters :
 ; YSDT - date of interest
 ; YSDFLT - default values for the search string (can be a code by default)
 ; YSOUT - local array to return results (passed as a reference)
 ; YSPARAM - parameters/string constants (see SETPARAM for details)
 ;returns ICD-10 code selected by the user:
 ;  IEN file #80;ICD code value^description
 ; results
 ; or -1 if invalid data(press enter)
 ; "" if not found
 ; or -2 if time out
 ; or -3 if ^ or ^^
 ; or -4 in ICD10 if the user answered NO for the question "Do you wish to continue(Y/N)?"
 ; 
DIAG10(YSDT,YSDFLT,YSPARAM) ;
 N YSINP
 S YSINP=$$SRCHSTR(YSPARAM("SEARCH_PROMPT"),YSPARAM("HELP ?"),YSPARAM("HELP ??"),YSDFLT)
 I YSINP<0 Q +YSINP
 I $P(YSINP,U,2)?.N Q $P(YSINP,U,2)_U_"LIST CHOICE"
 Q $$LEXICD10($P(YSINP,U,2),YSDT,.YSPARAM)
 ;
 ;//---------
 ;The entry point for ICD-9 FileMan type (^DIC) diagnosis search functionality
 ;can be called from applications directly
 ;input parameters :
 ; YSDT - date of interest
 ; YSDFLT - default values for the search string (can be a code by default)
 ; YSOUT - local array to return results(passed as a reference)
 ; YSPARAM - parameters/string constants (see SETPARAM for details)
 ;returns ICD-9 code selected by the user:
 ;  IEN file #80;ICD code value^description
 ;  -2 no data or was aborted
 ;  -1 if timeout
DIAG9(YSDT,YSDFLT,YSOUT,YSPARAM) ;
 N YSINP,YSRETV
 S YSINP=$$SRCHSTR(YSPARAM("SEARCH_PROMPT"),YSPARAM("HELP ?"),YSPARAM("HELP ??"),YSDFLT)
 I YSINP=-1 Q -1  ;enter
 I YSINP=-3 Q -1  ;^ or ^^
 I YSINP=-2 Q -2  ;timeout or not found
 I YSINP=-1!(YSINP=-3) Q -2
 I YSINP<0 Q +YSINP
 S YSRETV=$$ICD9($P(YSINP,U,2),YSDT,.YSOUT)
 I YSRETV=-1 Q -2
 Q YSRETV
 ;
 ;--------------
 ;The entry point for ICD-10 diagnosis search functionality
 ;can be called from applications directly
 ; Supported ICR 5681 ($$DIAGSRCH^LEX10CS)
 ;input parameters :
 ; YSTXT - search string
 ; YSDATE - date of interest
 ; YSPAR - array with text messages and other string constants
 ;returns ICD-10 code selected by the user:
 ;  IEN file #80;ICD code value^description
 ; or
 ; "" if not found
 ; -1 if exit : ^ or ^^
 ; -2 if continue searching
 ;
LEXICD10(YSTXT,YSDATE,YSPAR) ; ICD-10 Search
 N YSLVTXT
 ;parameters check
 S YSDATE=+$G(YSDATE)
 S YSDATE=$P(YSDATE,".",1)
 I YSDATE'?7N Q -1
 S YSTXT=$G(YSTXT)
 Q:'$L(YSTXT) -1
 N YSNUMB
 S YSNUMB=$$FREQ^LEXU(YSTXT)
 I YSNUMB>$$MAX^LEXU(30) D  I $$QUESTION("N",YSPARAM("WISH CONTINUE"))'=1 Q -4
 . W ! D FORMWRIT(YSPAR("EXCEEDS MESSAGE1")_YSTXT_YSPAR("EXCEEDS MESSAGE2")_YSNUMB_YSPAR("EXCEEDS MESSAGE3")_YSTXT_""".",0)
 . D FORMWRIT("",2) W !
 ;new and set variables
 N DIROUT,DUOUT,DTOUT,YSEXIT,YSICDNT
 N YSRETV,YSXX,YSLEVEL
 S YSRETV=""
 S YSEXIT=0
 S YSLEVEL=1,YSLVTXT(YSLEVEL)=YSTXT ;level 1 stores the original search string
 ; main loop
 F  Q:YSEXIT>0  D
 .K YSICDY
 .;W !,"Level #: ",YSLEVEL,", search string: ",YSLVTXT(YSLEVEL)
 .;get the search string from the current level and call LEX API
 .S YSICDY=$$DIAGSRCH^LEX10CS(YSLVTXT(YSLEVEL),.YSICDY,YSDATE,30)
 .S:$O(YSICDY(" "),-1)>0 YSICDY=+YSICDY
 .; Nothing found
 .I +YSICDY'>0 S YSEXIT=1 S YSXX=-1 Q
 .; display the list of items and ask the user to select the item from the list
 .S YSXX=$$SEL^YSLXDG2(.YSICDY,8)
 .; if ^ was entered
 .;   if this is on the top level then quit
 .I YSXX=-2,YSLEVEL'>1 S YSRETV=-1 S YSEXIT=1 Q
 .;   if lower level then go one level up
 .I YSXX=-2,YSLEVEL>1 S:YSLEVEL>1 YSLEVEL=YSLEVEL-1 Q
 .; If timeout, or not selected, or ^^ then quit
 .I YSXX=-1 S YSRETV=-1 S YSEXIT=1 Q
 .; if Code Found and Selected by the user save selection in YSRETV and quit
 .I $P(YSXX,";")'="99:CAT" S YSRETV=YSXX S YSEXIT=1 Q
 .; If Category Found and Selected by the user:
 .;  go to the next inner level
 .;  change level number
 .S YSLEVEL=YSLEVEL+1
 .;  set the new level with the new search string
 .;  and repeat
 .S YSLVTXT(YSLEVEL)=$P($P($G(YSXX),"^"),";",2)
 Q YSRETV
 ;----------
 ;ICD-9 lookup (FileMan lookup)
 ;Supported ICR 5773 (FileMan lookup for files #80 nad #80.1)
 ;Supported ICR 5699 ($$ICDDATA^ICDXCODE)
 ;input parameters :
 ; YSSRCH - search string
 ; YSICDT - date of interest
 ; YSOUT - local array to return detailed info (passed as a reference)
 ;returns ICD-9 code selected by the user:
 ;  IEN file #80;ICD code value^description
 ; or
 ; "" if not found
 ; -1 if exit : ^ or ^^
 ; -2 if continue search
 ;the array YSOUT returns details if the return value >0, here is an example:
 ; YSOUT="6065^814.14"
 ; YSOUT(0)=814.14
 ; YSOUT(0,0)=814.14
 ; YSOUT(0,1)="6065^814.14^^FX PISIFORM-OPEN^^8^^1^^1^^^0^^^^2781001^^1^1"
 ; YSOUT(0,2)="OPEN FRACTURE OF PISIFORM BONE OF WRIST"
 ;Note: this API is not silent because the ICD lookup is not silent
ICD9(YSSRCH,YSICDT,YSOUT) ;
 N KEY,X,Y,DIC,YSCDS
 ;KEY must be newed as ICD lookup code doesn't kill it
 S DIC="^ICD9(",DIC(0)="EQXZ"
 S YSCDS="ICD9"
 ;note: you must use Y for the 2nd parameter of $$ICDDATA^ICDXCODE
 S DIC("S")="I $P($$ICDDATA^ICDXCODE(YSCDS,Y,YSICDT),U,10)=1"
 ; both X and Y should be set to the search string
 S (X,Y)=YSSRCH
 D ^DIC
 M YSOUT=Y
 I $G(Y) Q $S(Y=-1:-1,1:+Y_";"_$P(Y,U,2)_U_$G(Y(0,2)))
 Q X
 ;
 ;---------
 ; Clean up environment and quit
EXIT ;
 K %,DIC,DIR,DIROUT,DIRUT,DTOUT,DUOUT,X,Y
 Q
 ;
 ;-----------
 ; Look-up help for ICD10s.
INPHLP ; Help text controller for ICD-10
 I X["???" D QM3 Q
 I X["??" D QM2 Q
 I X["?" D QM1 Q
 Q
QM ; Diagnosis help text
QM1 ; simple help text for 1 question mark
 W !,"Enter code or ""text"" for more information.",!
 Q
QM2 ; enhanced help text for 2 question marks
 W !,"Enter a ""free text"" term or part of a term such as ""femur fracture"".",!
 W !,"  or",!
 W !,"Enter a ""classification code"" (ICD/CPT etc) to find the single term associated"
 W !,"with the code.",!
 W !," or",!
 W !,"Enter a ""partial code"". Include the decimal when a search criterion includes"
 W !,"3 characters or more for code searches.",!
 Q
QM3 ; further explanation of format when there are multiple returns, displayed for 3 question marks.
 W !,"Number of Code Matches"
 W !,"----------------------",!
 W !,"The ICD-10 Diagnosis Code search will show the user the number of matches"
 W !,"found, indicate if additional characters in ICD code exist, and the number"
 W !,"of codes within the category or subcategory that are available for selection."
 W !,"For example:",!
 W !,"14 matches found",!
 W !,"M91. -    Juvenile osteochondrosis of hip and pelvis (19)",!
 W !,"This indicates that 14 unique matches or matching groups have been found and"
 W !,"will be displayed.",!
 W !,"M91. -   the ""-"" indicates that there are additional characters that specify"
 W !,"         unique ICD-10 codes available.",!
 W !,"(19)     Indicates that there are 19 additional ICD-10 codes in the M91"
 W !,"         ""family"" that are possible selections.",!
 Q
 ;
MIN2 ; Minimum length of 2 characters message
 W $C(7),"  ??",!
 W !,"Please enter at least the first two characters of the ICD-10 code or "
 W !,"code description to start the search.",!
 Q
 ;
INPHLP2 ; Look-up help for ICD9s
 W !," Enter a ""free text"" term. Best results occur using one to "
 W !," three full or partial words without a suffix"
 W:$G(X)'["??" "."
 W:$G(X)["??" " (i.e., ""DIABETES"","
 W:$G(X)["??" !," ""DIAB MELL"",""DIAB MELL INSUL"")"
 W !," or "
 W !," Enter a classification code (ICD/CPT etc) to find the single "
 W !," term associated with the code."
 W:$G(X)["??" " Example, a lookup of code 239.0 "
 W:$G(X)["??" !," returns one and only one term, that is the preferred "
 W:$G(X)["??" !," term for the code 239.0, ""Neoplasm of unspecified nature "
 W:$G(X)["??" !," of digestive system"""
 W !," or "
 W !," Enter a classification code (ICD/CPT etc) followed by a plus"
 W !," sign (+) to retrieve all terms associated with the code."
 W:$G(X)["??" " Example,"
 W:$G(X)["??" !," a lookup of 239.0+ returns all terms that are linked to the "
 W:$G(X)["??" !," code 239.0."
 Q
 ;--------
 ;prompt the user for a date of interest
 ;input parameters :
 ; YSPRMT - prompt
 ;returns YYYMMDD
 ; or -1 if invalid date
 ; or -2 if time out
 ; or -3 if ^
ASKDATE(YSPRMT) ;
 N %DT,DIROUT,DUOUT,DTOUT
 S %DT="AEX",%DT("A")=$G(YSPRMT,"Enter a date: ")
 D ^%DT
 Q:Y<0 -1
 Q:$D(DTOUT) -2
 Q:X="^" -3
 Q (+Y)
 ;--------
 ;ask YES/NO questions
 ;input parameters :
 ; YSDFLT- 0/null- not default, 1- yes, 2 -no
 ; YSPROM - prompt string
 ;returns
 ; 2 - no,
 ; 1 -yes,
 ; 0 - no answer
QUESTION(YSDFLT,YSPROM) ;
 W:$L($G(YSPROM)) !,YSPROM
 S %=$G(YSDFLT,2)
 D YN^DICN
 Q:%Y["^" -3
 I %=2!(%=1) Q %
 Q -2
 ;
 ;------------
 ;get search string
 ;input parameters :
 ; YSPRMT prompt text
 ; YSHLP1 "?" help text
 ; YSHLP2 "??" help text
 ; YSDFLT- default response
 ;returns piece1 ^ piece 2
 ; piece1:
 ; 0 if normal input
 ; or -1 if invalid data
 ; or -2 if time out
 ; or -3 if ^
 ; piece2: string entered by the user
SRCHSTR(YSPRMT,YSHLP1,YSHLP2,YSDFLT) ;
SRCHST2 N DIR
 S DIR("A")=YSPRMT
 S DIR("?")=YSHLP1
 S DIR("??")=YSHLP2
 I $L($G(YSDFLT)) S DIR("B")=YSDFLT
 S DIR(0)="FAO^0:245"
 D ^DIR
 Q:$D(DTOUT) -2
 Q:$D(DUOUT) -3
 Q:Y["^" -3
 I $L(Y)=1 D MIN2 G SRCHST2
 Q:Y="" -1
 Q 0_U_Y
 ;
 ;----------
 ;Determines and returns ACTIVE coding system for DIAGNOSES based on date of interest
 ;input parameters :
 ; YSICDD - date of interest
 ; if date of interest is null, today's date will be assumed
 ;returns coding system
 ; as a pointer to the ICD CODING SYSTEM file #80.4 (suppported ICR 5780)
 ; 30  if ICD-10-CM is active system
 ; 1   if ICD-9-CM is active system
ICDSYSDG(YSICDD) ;
 N YSIMPDT
 S YSICDD=$S(YSICDD<0!($L($P(YSICDD,".",1))'=7):DT,1:+$G(YSICDD))
 S YSIMPDT=$$IMPDATE^LEXU("10D")
 Q $S(YSICDD'<YSIMPDT:30,1:1)
 ;
 ;set parameters
 ;edit these hardcoded strings that areused for prompts, messages and so on to adjust
 ;them to your applicaion's needs
 ;input parameters
 ; YSPAR - local array to sets and store string constants for your messages and prompts 
SETPARAM(YSPAR) ;
 S YSPAR("ASKDATE")="Date of interest? "
 S YSPAR("SEARCH_PROMPT")="Enter ICD-10 DIAGNOSIS: "   ; assume ICD-10
 S YSPAR("HELP ?")="^D INPHLP^YSLXDG"
 S YSPAR("HELP ??")="^D INPHLP^YSLXDG"
 S YSPAR("NO DATA FOUND")="  No data found"
 S YSPAR("EXITING")="  Exiting"
 S YSPAR("TRY LATER")="  Try again later"
 S YSPAR("NO DATA SELECTED")="  No data selected"
 S YSPAR("TRY ANOTHER")="Try another"
 S YSPAR("WISH CONTINUE")="Do you wish to continue (Y/N)"
 S YSPAR("EXCEEDS MESSAGE1")="Searching for """
 S YSPAR("EXCEEDS MESSAGE2")=""" requires inspecting "
 S YSPAR("EXCEEDS MESSAGE3")=" records to determine if they match the search criteria.  This could take quite some time.  Suggest refining the search by further specifying """
 Q
 ;
 ;
 ;a wrapper for ^DIWP
 ;accumulates a text and then writes it to the device
 ;input parameters :
 ; X - text
 ; YSMODE:
 ;  0 - start
 ;  1 - accumulate
 ;  2 - write
 ;example:
 ;D FORMWRIT^ZZLXDG("this API is a wrapper for ^DIWP, it accumulates a text and then writes it to the device, you can use it in your application code",0)
 ;D FORMWRIT^ZZLXDG("some more text ",1)
 ;D FORMWRIT^ZZLXDG("",2)
FORMWRIT(X,YSMODE) ;
 N YSLI1
 ;if "start" mode
 I YSMODE=0 K ^UTILITY($J,"W")
 S DIWL=1,DIWR=79
 I $L(X)>0 D ^DIWP
 ;if "write" mode
 I YSMODE=2 D
 . S YSLI1=0 F  S YSLI1=$O(^UTILITY($J,"W",1,YSLI1)) Q:+YSLI1=0  W !,$G(^UTILITY($J,"W",1,YSLI1,0))
 . K ^UTILITY($J,"W")
 Q
 ;
 ;---------------
 ;Initialize variables if you need , your application most likely already has this
INITVARS ;
 D HOME^%ZIS
 S:$G(DT)=0 DT=$$DT^XLFDT
 Q
 ;press any key
PRESSKEY ;
 R !!,"Press any key to continue.",YSKEY:DTIME
 Q
 ;display code info
CODEINFO(YSXX2) ; Write Output
 N YSKEY,YSICDSTR
 S YSICDSTR="ICD"_$S(YSCSYS="30":"10",1:"9")
 N YSTXT,YSI S YSTXT(1)=$P($P(YSXX2,";",2),U,2)
 D PR^YSLXDG2(.YSTXT,48)
 W !," ",YSICDSTR," Diagnosis code:",?31,$P($P(YSXX2,";",2),U,1)
 W !," ",YSICDSTR," Diagnosis description:",?31,YSTXT(1)
 S YSI=1 F  S YSI=$O(YSTXT(YSI)) Q:+YSI'>0  W !,?31,$G(YSTXT(YSI))
 Q
 ;
 
--- Routine Detail   --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HYSLXDG   15155     printed  Sep 23, 2025@19:50:47                                                                                                                                                                                                     Page 2
YSLXDG    ; ALB/RBD - ICD-10 DIAGNOSIS CODE LOOK UP FOR MENTAL HEALTH ;10 May 2013  11:17 AM
 +1       ;;5.01;MENTAL HEALTH;**107**;Dec 30, 1994;Build 23
 +2       ;
 +3       ;based on ^ZZLXDG which is the standard Diagnosis Search Protocol
 +4       ;beginning routine.
 +5       ;
 +6        QUIT 
 +7       ;
EN        ;
 +1       ;set standards variables, you might not need this if it
           DO INITVARS
 +2       ; was already done in your application
 +3       ; to manage loop
           NEW YSQUIT
 +4       ;to store the selected code information
           KILL YSRETV
 +5       ;  to set your application specific prompts and messages
           NEW YSPARAM
 +6       ;coding system "ICD9" or ICD10"
           NEW YSCSYS
 +7       ;to return all available information about the selected code
           NEW YSOUT
 +8       ;settings:
 +9       ;edit the SETPARAM subroutine below to set your
           DO SETPARAM(.YSPARAM)
 +10      ; application specific prompts
 +11       IF YSDT'>0
               SET YSRETV=-1
               QUIT 
 +12      ;starting main loop
 +13       SET YSQUIT=0
           FOR 
               if YSQUIT=1
                   QUIT 
               Begin DoDot:1
 +14               SET YSRETV=0
                   SET YSOUT=""
 +15      ;reprompt a few lines down
                   WRITE !!
 +16      ;prompt for the date of interest (date should be available for MH)
 +17               IF YSDT'>0
                       SET YSRETV=-1
                       SET YSQUIT=1
                       QUIT 
 +18      ;S YSDT=$$ASKDATE(YSPARAM("ASKDATE"))
 +19      ;prompt for "try again" with "No" as default if ^ or null entered
 +20      ;for the date or if timed out
 +21               IF YSDT'>0
                       if $$QUESTION(2,YSPARAM("TRY ANOTHER"))'=1
                           SET YSQUIT=1
                       QUIT 
 +22      ;determine coding system based on the date of interest
 +23      ;If coding system not ICD-10 or greater, then Quit (let MH code
 +24      ; handle it as before for now)
 +25               SET YSCSYS=$$ICDSYSDG(YSDT)
                   IF YSCSYS=1
                       SET YSRETV=-1
                       SET YSQUIT=1
                       QUIT 
 +26      ;set default response for your prompt
 +27               SET YSDFLT=""
 +28      ;run either ICD9 or ICD10 prompt/search/select logic
 +29      ;ICD9 (1 is a pointer to the ICD-9 diagnosis system entry in the
 +30      ;new file #80.4)
 +31               IF YSCSYS=1
                       SET YSRETV=$$DIAG9(YSDT,YSDFLT,.YSOUT,.YSPARAM)
                       IF YSRETV=-2
                           if $$QUESTION(1,YSPARAM("TRY ANOTHER"))'=1
                               SET YSQUIT=1
                           QUIT 
 +32      ;ICD10 (30 is a pointer to the ICD-10 diagnosis system entry in the new file #80.4)
 +33               IF YSCSYS=30
                       SET YSRETV=$$DIAG10(YSDT,YSDFLT,.YSPARAM)
 +34               IF $PIECE(YSRETV,U,2)="LIST CHOICE"
                       SET YSRETV=$PIECE(YSRETV,U,1)
                       SET YSQUIT=1
                       QUIT 
 +35      ;display information about the code selected
 +36               IF YSRETV>0
                       WRITE !,"SELECTED: "
                       DO CODEINFO(YSRETV)
                       SET YSQUIT=1
                       QUIT 
 +37      ;if no data found
 +38               IF YSRETV=""
                       WRITE !!,YSPARAM("NO DATA FOUND")
                       if $$QUESTION(1,YSPARAM("TRY ANOTHER"))'=1
                           SET YSQUIT=1
                           SET YSRETV=-1
                       QUIT 
 +39      ;in ICD10 if the user answered NO for the question "Do you wish to continue(Y/N)?"
 +40               IF YSRETV=-4
                       if $$QUESTION(1,YSPARAM("TRY ANOTHER"))'=1
                           SET YSQUIT=1
                       QUIT 
 +41      ;no data or was aborted
 +42               IF YSRETV=-2
                       if $$QUESTION(1,YSPARAM("TRY ANOTHER"))'=1
                           SET YSQUIT=1
                       QUIT 
 +43      ;if exit due to ^ in the ICD Diagnosis code prompt
 +44               IF YSRETV=-3
                       if $$QUESTION(2,YSPARAM("TRY ANOTHER"))'=1
                           SET YSQUIT=1
                       QUIT 
 +45      ;if no data found
 +46               IF YSRETV=-1
                       if $$QUESTION(2,YSPARAM("TRY ANOTHER"))'=1
                           SET YSQUIT=1
                       QUIT 
 +47      ; if continue search
               End DoDot:1
 +48       QUIT 
 +49      ;
 +50      ;//---------
 +51      ;The entry point for ICD-10 diagnosis search functionality
 +52      ;can be called from applications directly
 +53      ;input parameters :
 +54      ; YSDT - date of interest
 +55      ; YSDFLT - default values for the search string (can be a code by default)
 +56      ; YSOUT - local array to return results (passed as a reference)
 +57      ; YSPARAM - parameters/string constants (see SETPARAM for details)
 +58      ;returns ICD-10 code selected by the user:
 +59      ;  IEN file #80;ICD code value^description
 +60      ; results
 +61      ; or -1 if invalid data(press enter)
 +62      ; "" if not found
 +63      ; or -2 if time out
 +64      ; or -3 if ^ or ^^
 +65      ; or -4 in ICD10 if the user answered NO for the question "Do you wish to continue(Y/N)?"
 +66      ; 
DIAG10(YSDT,YSDFLT,YSPARAM) ;
 +1        NEW YSINP
 +2        SET YSINP=$$SRCHSTR(YSPARAM("SEARCH_PROMPT"),YSPARAM("HELP ?"),YSPARAM("HELP ??"),YSDFLT)
 +3        IF YSINP<0
               QUIT +YSINP
 +4        IF $PIECE(YSINP,U,2)?.N
               QUIT $PIECE(YSINP,U,2)_U_"LIST CHOICE"
 +5        QUIT $$LEXICD10($PIECE(YSINP,U,2),YSDT,.YSPARAM)
 +6       ;
 +7       ;//---------
 +8       ;The entry point for ICD-9 FileMan type (^DIC) diagnosis search functionality
 +9       ;can be called from applications directly
 +10      ;input parameters :
 +11      ; YSDT - date of interest
 +12      ; YSDFLT - default values for the search string (can be a code by default)
 +13      ; YSOUT - local array to return results(passed as a reference)
 +14      ; YSPARAM - parameters/string constants (see SETPARAM for details)
 +15      ;returns ICD-9 code selected by the user:
 +16      ;  IEN file #80;ICD code value^description
 +17      ;  -2 no data or was aborted
 +18      ;  -1 if timeout
DIAG9(YSDT,YSDFLT,YSOUT,YSPARAM) ;
 +1        NEW YSINP,YSRETV
 +2        SET YSINP=$$SRCHSTR(YSPARAM("SEARCH_PROMPT"),YSPARAM("HELP ?"),YSPARAM("HELP ??"),YSDFLT)
 +3       ;enter
           IF YSINP=-1
               QUIT -1
 +4       ;^ or ^^
           IF YSINP=-3
               QUIT -1
 +5       ;timeout or not found
           IF YSINP=-2
               QUIT -2
 +6        IF YSINP=-1!(YSINP=-3)
               QUIT -2
 +7        IF YSINP<0
               QUIT +YSINP
 +8        SET YSRETV=$$ICD9($PIECE(YSINP,U,2),YSDT,.YSOUT)
 +9        IF YSRETV=-1
               QUIT -2
 +10       QUIT YSRETV
 +11      ;
 +12      ;--------------
 +13      ;The entry point for ICD-10 diagnosis search functionality
 +14      ;can be called from applications directly
 +15      ; Supported ICR 5681 ($$DIAGSRCH^LEX10CS)
 +16      ;input parameters :
 +17      ; YSTXT - search string
 +18      ; YSDATE - date of interest
 +19      ; YSPAR - array with text messages and other string constants
 +20      ;returns ICD-10 code selected by the user:
 +21      ;  IEN file #80;ICD code value^description
 +22      ; or
 +23      ; "" if not found
 +24      ; -1 if exit : ^ or ^^
 +25      ; -2 if continue searching
 +26      ;
LEXICD10(YSTXT,YSDATE,YSPAR) ; ICD-10 Search
 +1        NEW YSLVTXT
 +2       ;parameters check
 +3        SET YSDATE=+$GET(YSDATE)
 +4        SET YSDATE=$PIECE(YSDATE,".",1)
 +5        IF YSDATE'?7N
               QUIT -1
 +6        SET YSTXT=$GET(YSTXT)
 +7        if '$LENGTH(YSTXT)
               QUIT -1
 +8        NEW YSNUMB
 +9        SET YSNUMB=$$FREQ^LEXU(YSTXT)
 +10       IF YSNUMB>$$MAX^LEXU(30)
               Begin DoDot:1
 +11               WRITE !
                   DO FORMWRIT(YSPAR("EXCEEDS MESSAGE1")_YSTXT_YSPAR("EXCEEDS MESSAGE2")_YSNUMB_YSPAR("EXCEEDS MESSAGE3")_YSTXT_""".",0)
 +12               DO FORMWRIT("",2)
                   WRITE !
               End DoDot:1
               IF $$QUESTION("N",YSPARAM("WISH CONTINUE"))'=1
                   QUIT -4
 +13      ;new and set variables
 +14       NEW DIROUT,DUOUT,DTOUT,YSEXIT,YSICDNT
 +15       NEW YSRETV,YSXX,YSLEVEL
 +16       SET YSRETV=""
 +17       SET YSEXIT=0
 +18      ;level 1 stores the original search string
           SET YSLEVEL=1
           SET YSLVTXT(YSLEVEL)=YSTXT
 +19      ; main loop
 +20       FOR 
               if YSEXIT>0
                   QUIT 
               Begin DoDot:1
 +21               KILL YSICDY
 +22      ;W !,"Level #: ",YSLEVEL,", search string: ",YSLVTXT(YSLEVEL)
 +23      ;get the search string from the current level and call LEX API
 +24               SET YSICDY=$$DIAGSRCH^LEX10CS(YSLVTXT(YSLEVEL),.YSICDY,YSDATE,30)
 +25               if $ORDER(YSICDY(" "),-1)>0
                       SET YSICDY=+YSICDY
 +26      ; Nothing found
 +27               IF +YSICDY'>0
                       SET YSEXIT=1
                       SET YSXX=-1
                       QUIT 
 +28      ; display the list of items and ask the user to select the item from the list
 +29               SET YSXX=$$SEL^YSLXDG2(.YSICDY,8)
 +30      ; if ^ was entered
 +31      ;   if this is on the top level then quit
 +32               IF YSXX=-2
                       IF YSLEVEL'>1
                           SET YSRETV=-1
                           SET YSEXIT=1
                           QUIT 
 +33      ;   if lower level then go one level up
 +34               IF YSXX=-2
                       IF YSLEVEL>1
                           if YSLEVEL>1
                               SET YSLEVEL=YSLEVEL-1
                           QUIT 
 +35      ; If timeout, or not selected, or ^^ then quit
 +36               IF YSXX=-1
                       SET YSRETV=-1
                       SET YSEXIT=1
                       QUIT 
 +37      ; if Code Found and Selected by the user save selection in YSRETV and quit
 +38               IF $PIECE(YSXX,";")'="99:CAT"
                       SET YSRETV=YSXX
                       SET YSEXIT=1
                       QUIT 
 +39      ; If Category Found and Selected by the user:
 +40      ;  go to the next inner level
 +41      ;  change level number
 +42               SET YSLEVEL=YSLEVEL+1
 +43      ;  set the new level with the new search string
 +44      ;  and repeat
 +45               SET YSLVTXT(YSLEVEL)=$PIECE($PIECE($GET(YSXX),"^"),";",2)
               End DoDot:1
 +46       QUIT YSRETV
 +47      ;----------
 +48      ;ICD-9 lookup (FileMan lookup)
 +49      ;Supported ICR 5773 (FileMan lookup for files #80 nad #80.1)
 +50      ;Supported ICR 5699 ($$ICDDATA^ICDXCODE)
 +51      ;input parameters :
 +52      ; YSSRCH - search string
 +53      ; YSICDT - date of interest
 +54      ; YSOUT - local array to return detailed info (passed as a reference)
 +55      ;returns ICD-9 code selected by the user:
 +56      ;  IEN file #80;ICD code value^description
 +57      ; or
 +58      ; "" if not found
 +59      ; -1 if exit : ^ or ^^
 +60      ; -2 if continue search
 +61      ;the array YSOUT returns details if the return value >0, here is an example:
 +62      ; YSOUT="6065^814.14"
 +63      ; YSOUT(0)=814.14
 +64      ; YSOUT(0,0)=814.14
 +65      ; YSOUT(0,1)="6065^814.14^^FX PISIFORM-OPEN^^8^^1^^1^^^0^^^^2781001^^1^1"
 +66      ; YSOUT(0,2)="OPEN FRACTURE OF PISIFORM BONE OF WRIST"
 +67      ;Note: this API is not silent because the ICD lookup is not silent
ICD9(YSSRCH,YSICDT,YSOUT) ;
 +1        NEW KEY,X,Y,DIC,YSCDS
 +2       ;KEY must be newed as ICD lookup code doesn't kill it
 +3        SET DIC="^ICD9("
           SET DIC(0)="EQXZ"
 +4        SET YSCDS="ICD9"
 +5       ;note: you must use Y for the 2nd parameter of $$ICDDATA^ICDXCODE
 +6        SET DIC("S")="I $P($$ICDDATA^ICDXCODE(YSCDS,Y,YSICDT),U,10)=1"
 +7       ; both X and Y should be set to the search string
 +8        SET (X,Y)=YSSRCH
 +9        DO ^DIC
 +10       MERGE YSOUT=Y
 +11       IF $GET(Y)
               QUIT $SELECT(Y=-1:-1,1:+Y_";"_$PIECE(Y,U,2)_U_$GET(Y(0,2)))
 +12       QUIT X
 +13      ;
 +14      ;---------
 +15      ; Clean up environment and quit
EXIT      ;
 +1        KILL %,DIC,DIR,DIROUT,DIRUT,DTOUT,DUOUT,X,Y
 +2        QUIT 
 +3       ;
 +4       ;-----------
 +5       ; Look-up help for ICD10s.
INPHLP    ; Help text controller for ICD-10
 +1        IF X["???"
               DO QM3
               QUIT 
 +2        IF X["??"
               DO QM2
               QUIT 
 +3        IF X["?"
               DO QM1
               QUIT 
 +4        QUIT 
QM        ; Diagnosis help text
QM1       ; simple help text for 1 question mark
 +1        WRITE !,"Enter code or ""text"" for more information.",!
 +2        QUIT 
QM2       ; enhanced help text for 2 question marks
 +1        WRITE !,"Enter a ""free text"" term or part of a term such as ""femur fracture"".",!
 +2        WRITE !,"  or",!
 +3        WRITE !,"Enter a ""classification code"" (ICD/CPT etc) to find the single term associated"
 +4        WRITE !,"with the code.",!
 +5        WRITE !," or",!
 +6        WRITE !,"Enter a ""partial code"". Include the decimal when a search criterion includes"
 +7        WRITE !,"3 characters or more for code searches.",!
 +8        QUIT 
QM3       ; further explanation of format when there are multiple returns, displayed for 3 question marks.
 +1        WRITE !,"Number of Code Matches"
 +2        WRITE !,"----------------------",!
 +3        WRITE !,"The ICD-10 Diagnosis Code search will show the user the number of matches"
 +4        WRITE !,"found, indicate if additional characters in ICD code exist, and the number"
 +5        WRITE !,"of codes within the category or subcategory that are available for selection."
 +6        WRITE !,"For example:",!
 +7        WRITE !,"14 matches found",!
 +8        WRITE !,"M91. -    Juvenile osteochondrosis of hip and pelvis (19)",!
 +9        WRITE !,"This indicates that 14 unique matches or matching groups have been found and"
 +10       WRITE !,"will be displayed.",!
 +11       WRITE !,"M91. -   the ""-"" indicates that there are additional characters that specify"
 +12       WRITE !,"         unique ICD-10 codes available.",!
 +13       WRITE !,"(19)     Indicates that there are 19 additional ICD-10 codes in the M91"
 +14       WRITE !,"         ""family"" that are possible selections.",!
 +15       QUIT 
 +16      ;
MIN2      ; Minimum length of 2 characters message
 +1        WRITE $CHAR(7),"  ??",!
 +2        WRITE !,"Please enter at least the first two characters of the ICD-10 code or "
 +3        WRITE !,"code description to start the search.",!
 +4        QUIT 
 +5       ;
INPHLP2   ; Look-up help for ICD9s
 +1        WRITE !," Enter a ""free text"" term. Best results occur using one to "
 +2        WRITE !," three full or partial words without a suffix"
 +3        if $GET(X)'["??"
               WRITE "."
 +4        if $GET(X)["??"
               WRITE " (i.e., ""DIABETES"","
 +5        if $GET(X)["??"
               WRITE !," ""DIAB MELL"",""DIAB MELL INSUL"")"
 +6        WRITE !," or "
 +7        WRITE !," Enter a classification code (ICD/CPT etc) to find the single "
 +8        WRITE !," term associated with the code."
 +9        if $GET(X)["??"
               WRITE " Example, a lookup of code 239.0 "
 +10       if $GET(X)["??"
               WRITE !," returns one and only one term, that is the preferred "
 +11       if $GET(X)["??"
               WRITE !," term for the code 239.0, ""Neoplasm of unspecified nature "
 +12       if $GET(X)["??"
               WRITE !," of digestive system"""
 +13       WRITE !," or "
 +14       WRITE !," Enter a classification code (ICD/CPT etc) followed by a plus"
 +15       WRITE !," sign (+) to retrieve all terms associated with the code."
 +16       if $GET(X)["??"
               WRITE " Example,"
 +17       if $GET(X)["??"
               WRITE !," a lookup of 239.0+ returns all terms that are linked to the "
 +18       if $GET(X)["??"
               WRITE !," code 239.0."
 +19       QUIT 
 +20      ;--------
 +21      ;prompt the user for a date of interest
 +22      ;input parameters :
 +23      ; YSPRMT - prompt
 +24      ;returns YYYMMDD
 +25      ; or -1 if invalid date
 +26      ; or -2 if time out
 +27      ; or -3 if ^
ASKDATE(YSPRMT) ;
 +1        NEW %DT,DIROUT,DUOUT,DTOUT
 +2        SET %DT="AEX"
           SET %DT("A")=$GET(YSPRMT,"Enter a date: ")
 +3        DO ^%DT
 +4        if Y<0
               QUIT -1
 +5        if $DATA(DTOUT)
               QUIT -2
 +6        if X="^"
               QUIT -3
 +7        QUIT (+Y)
 +8       ;--------
 +9       ;ask YES/NO questions
 +10      ;input parameters :
 +11      ; YSDFLT- 0/null- not default, 1- yes, 2 -no
 +12      ; YSPROM - prompt string
 +13      ;returns
 +14      ; 2 - no,
 +15      ; 1 -yes,
 +16      ; 0 - no answer
QUESTION(YSDFLT,YSPROM) ;
 +1        if $LENGTH($GET(YSPROM))
               WRITE !,YSPROM
 +2        SET %=$GET(YSDFLT,2)
 +3        DO YN^DICN
 +4        if %Y["^"
               QUIT -3
 +5        IF %=2!(%=1)
               QUIT %
 +6        QUIT -2
 +7       ;
 +8       ;------------
 +9       ;get search string
 +10      ;input parameters :
 +11      ; YSPRMT prompt text
 +12      ; YSHLP1 "?" help text
 +13      ; YSHLP2 "??" help text
 +14      ; YSDFLT- default response
 +15      ;returns piece1 ^ piece 2
 +16      ; piece1:
 +17      ; 0 if normal input
 +18      ; or -1 if invalid data
 +19      ; or -2 if time out
 +20      ; or -3 if ^
 +21      ; piece2: string entered by the user
SRCHSTR(YSPRMT,YSHLP1,YSHLP2,YSDFLT) ;
SRCHST2    NEW DIR
 +1        SET DIR("A")=YSPRMT
 +2        SET DIR("?")=YSHLP1
 +3        SET DIR("??")=YSHLP2
 +4        IF $LENGTH($GET(YSDFLT))
               SET DIR("B")=YSDFLT
 +5        SET DIR(0)="FAO^0:245"
 +6        DO ^DIR
 +7        if $DATA(DTOUT)
               QUIT -2
 +8        if $DATA(DUOUT)
               QUIT -3
 +9        if Y["^"
               QUIT -3
 +10       IF $LENGTH(Y)=1
               DO MIN2
               GOTO SRCHST2
 +11       if Y=""
               QUIT -1
 +12       QUIT 0_U_Y
 +13      ;
 +14      ;----------
 +15      ;Determines and returns ACTIVE coding system for DIAGNOSES based on date of interest
 +16      ;input parameters :
 +17      ; YSICDD - date of interest
 +18      ; if date of interest is null, today's date will be assumed
 +19      ;returns coding system
 +20      ; as a pointer to the ICD CODING SYSTEM file #80.4 (suppported ICR 5780)
 +21      ; 30  if ICD-10-CM is active system
 +22      ; 1   if ICD-9-CM is active system
ICDSYSDG(YSICDD) ;
 +1        NEW YSIMPDT
 +2        SET YSICDD=$SELECT(YSICDD<0!($LENGTH($PIECE(YSICDD,".",1))'=7):DT,1:+$GET(YSICDD))
 +3        SET YSIMPDT=$$IMPDATE^LEXU("10D")
 +4        QUIT $SELECT(YSICDD'<YSIMPDT:30,1:1)
 +5       ;
 +6       ;set parameters
 +7       ;edit these hardcoded strings that areused for prompts, messages and so on to adjust
 +8       ;them to your applicaion's needs
 +9       ;input parameters
 +10      ; YSPAR - local array to sets and store string constants for your messages and prompts 
SETPARAM(YSPAR) ;
 +1        SET YSPAR("ASKDATE")="Date of interest? "
 +2       ; assume ICD-10
           SET YSPAR("SEARCH_PROMPT")="Enter ICD-10 DIAGNOSIS: "
 +3        SET YSPAR("HELP ?")="^D INPHLP^YSLXDG"
 +4        SET YSPAR("HELP ??")="^D INPHLP^YSLXDG"
 +5        SET YSPAR("NO DATA FOUND")="  No data found"
 +6        SET YSPAR("EXITING")="  Exiting"
 +7        SET YSPAR("TRY LATER")="  Try again later"
 +8        SET YSPAR("NO DATA SELECTED")="  No data selected"
 +9        SET YSPAR("TRY ANOTHER")="Try another"
 +10       SET YSPAR("WISH CONTINUE")="Do you wish to continue (Y/N)"
 +11       SET YSPAR("EXCEEDS MESSAGE1")="Searching for """
 +12       SET YSPAR("EXCEEDS MESSAGE2")=""" requires inspecting "
 +13       SET YSPAR("EXCEEDS MESSAGE3")=" records to determine if they match the search criteria.  This could take quite some time.  Suggest refining the search by further specifying """
 +14       QUIT 
 +15      ;
 +16      ;
 +17      ;a wrapper for ^DIWP
 +18      ;accumulates a text and then writes it to the device
 +19      ;input parameters :
 +20      ; X - text
 +21      ; YSMODE:
 +22      ;  0 - start
 +23      ;  1 - accumulate
 +24      ;  2 - write
 +25      ;example:
 +26      ;D FORMWRIT^ZZLXDG("this API is a wrapper for ^DIWP, it accumulates a text and then writes it to the device, you can use it in your application code",0)
 +27      ;D FORMWRIT^ZZLXDG("some more text ",1)
 +28      ;D FORMWRIT^ZZLXDG("",2)
FORMWRIT(X,YSMODE) ;
 +1        NEW YSLI1
 +2       ;if "start" mode
 +3        IF YSMODE=0
               KILL ^UTILITY($JOB,"W")
 +4        SET DIWL=1
           SET DIWR=79
 +5        IF $LENGTH(X)>0
               DO ^DIWP
 +6       ;if "write" mode
 +7        IF YSMODE=2
               Begin DoDot:1
 +8                SET YSLI1=0
                   FOR 
                       SET YSLI1=$ORDER(^UTILITY($JOB,"W",1,YSLI1))
                       if +YSLI1=0
                           QUIT 
                       WRITE !,$GET(^UTILITY($JOB,"W",1,YSLI1,0))
 +9                KILL ^UTILITY($JOB,"W")
               End DoDot:1
 +10       QUIT 
 +11      ;
 +12      ;---------------
 +13      ;Initialize variables if you need , your application most likely already has this
INITVARS  ;
 +1        DO HOME^%ZIS
 +2        if $GET(DT)=0
               SET DT=$$DT^XLFDT
 +3        QUIT 
 +4       ;press any key
PRESSKEY  ;
 +1        READ !!,"Press any key to continue.",YSKEY:DTIME
 +2        QUIT 
 +3       ;display code info
CODEINFO(YSXX2) ; Write Output
 +1        NEW YSKEY,YSICDSTR
 +2        SET YSICDSTR="ICD"_$SELECT(YSCSYS="30":"10",1:"9")
 +3        NEW YSTXT,YSI
           SET YSTXT(1)=$PIECE($PIECE(YSXX2,";",2),U,2)
 +4        DO PR^YSLXDG2(.YSTXT,48)
 +5        WRITE !," ",YSICDSTR," Diagnosis code:",?31,$PIECE($PIECE(YSXX2,";",2),U,1)
 +6        WRITE !," ",YSICDSTR," Diagnosis description:",?31,YSTXT(1)
 +7        SET YSI=1
           FOR 
               SET YSI=$ORDER(YSTXT(YSI))
               if +YSI'>0
                   QUIT 
               WRITE !,?31,$GET(YSTXT(YSI))
 +8        QUIT 
 +9       ;