Home   Package List   Routine Alphabetical List   Global Alphabetical List   FileMan Files List   FileMan Sub-Files List   Package Component Lists   Package-Namespace Mapping  
Routine: PSSDSUTL

PSSDSUTL.m

Go to the documentation of this file.
PSSDSUTL ;BIR/MV-Dose Check utility routine (continued) ;27 Oct 2009  12:22 PM
 ;;1.0;PHARMACY DATA MANAGEMENT;**201,178,206,224,231**;9/30/97;Build 4
 ;
RANGE ;Evaluate free text dosages for range patterns
 N PSSRG1,PSSRG2,PSSRG3,PSSRG4,PSSRG5,PSSRG6,PSSRGAR,PSSRGDOS,PSSRGLT,PSSRGNM1,PSSRGNM2,PSSRGUN1,PSSRGUN2
 S PSSRG2=0,PSSRGDOS=$G(PSSDSLCL)
 S PSSRGDOS=$$UP^XLFSTR(PSSRGDOS)
 S PSSRGLT=$L(PSSRGDOS) I PSSRGLT'>3!(PSSRGDOS["  ") Q
 F PSSRG1=1:1:PSSRGLT Q:PSSRG2>4  I $E(PSSRGDOS,PSSRG1)=" " S PSSRG2=PSSRG2+1
 I PSSRG2>4 Q
 S PSSRGDOS=$TR(PSSRGDOS," ","") Q:$L(PSSRGDOS)'>0  ;Remove all spaces
 ;Derive leading numeric value
 I $E(PSSRGDOS)'?1N,$E(PSSRGDOS)'?1"." Q
 I $E(PSSRGDOS)?1".",$E(PSSRGDOS,2)'?1N Q
 S PSSRG2=0 F PSSRG3=1:1:$L(PSSRGDOS) Q:PSSRG2  S PSSRG4=$E(PSSRGDOS,PSSRG3) I PSSRG4'?1N,PSSRG4'?1".",PSSRG4'?1"," S PSSRG2=PSSRG3
 Q:'PSSRG2  ;only a numeric passed in
 S PSSRGNM1=$E(PSSRGDOS,1,(PSSRG2-1)),PSSRGDOS=$E(PSSRGDOS,PSSRG2,$L(PSSRGDOS)) Q:$L(PSSRGDOS)'>2
 S PSSRGAR=$E(PSSRGNM1,$L(PSSRGNM1)) I PSSRGAR="."!(PSSRGAR=",") Q
 S PSSRG2=0 F PSSRG3=1:1:$L(PSSRGNM1) Q:PSSRG2>1  S PSSRG5=$E(PSSRGNM1,PSSRG3) S:PSSRG5="." PSSRG2=PSSRG2+1
 Q:PSSRG2>1
 S PSSRGNM1=$TR(PSSRGNM1,",","") ;PSSRGNM1 Set to first numeric
 S (PSSRG2,PSSRG4,PSSRG5)=0 F PSSRG3=1:1:$L(PSSRGDOS) Q:PSSRG2  S PSSRG4=$E(PSSRGDOS,PSSRG3) D:'PSSRG5  I PSSRG5,PSSRG4'?1N,PSSRG4'?1",",PSSRG4'?1"." S PSSRG2=PSSRG3
 .I PSSRG4?1N!(PSSRG4?1".") S (PSSRG5,PSSRG6)=PSSRG3
 I 'PSSRG5 Q
 I PSSRG2 S PSSRGUN2=$E(PSSRGDOS,PSSRG2,$L(PSSRGDOS)),PSSRGNM2=$E(PSSRGDOS,PSSRG5,(PSSRG2-1)) ;PSSRGUN2 set to second Unit if there is one
 I 'PSSRG2 S PSSRGNM2=$E(PSSRGDOS,PSSRG5,$L(PSSRGDOS))
 I $E(PSSRGNM2)'?1N,$E(PSSRGNM2)'?1"." Q
 I $E(PSSRGNM2)?1".",$E(PSSRGNM2,2)'?1N Q
 S PSSRGAR=$E(PSSRGNM2,$L(PSSRGNM2)) I PSSRGAR="."!(PSSRGAR=",") Q
 S PSSRG2=0 F PSSRG3=1:1:$L(PSSRGNM2) Q:PSSRG2>1  S PSSRG5=$E(PSSRGNM2,PSSRG3) S:PSSRG5="." PSSRG2=PSSRG2+1
 Q:PSSRG2>1
 S PSSRGNM2=$TR(PSSRGNM2,",","") ;PSSRGNM2 Set to second numeric
 S PSSRGDOS=$E(PSSRGDOS,1,(PSSRG6-1))
 S PSSRG6=$L(PSSRGDOS) Q:PSSRG6'>0
 S PSSRG1=$E(PSSRGDOS,PSSRG6)
 I PSSRG1="-" S:PSSRG6>1 PSSRGUN1=$E(PSSRGDOS,1,(PSSRG6-1)) D RANGEOK Q
 S PSSRG2=$E(PSSRGDOS,(PSSRG6-1),PSSRG6) I PSSRG2="TO"!(PSSRG2="OR") S:PSSRG6>2 PSSRGUN1=$E(PSSRGDOS,1,(PSSRG6-2)) D RANGEOK
 Q
 ;
 ;
RANGEOK ;Retrieved all needed Dosing
 N PSSRGUNA,PSSRGUNB
 I PSSRGNM2'>PSSRGNM1 Q
 I PSSDBIFL D  Q
 .I $G(PSSRGUN1)'="" S PSSRGUNA=$$UNITD^PSSDSAPI(PSSRGUN1)
 .I $G(PSSRGUN2)'="" S PSSRGUNB=$$UNITD^PSSDSAPI(PSSRGUN2)
 .I $G(PSSRGUN1)'="",$G(PSSRGUN2)'="" D  Q
 ..I $G(PSSRGUNA)'="",PSSRGUNA=$G(PSSRGUNB) S PSSDBAR("AMN")=PSSRGNM2,PSSDBAR("UNIT")=PSSRGUNA,PSSDBFAL=1
 .I $G(PSSRGUN1)="" D
 ..I $G(PSSRGUNB)'="" S PSSDBAR("AMN")=PSSRGNM2,PSSDBAR("UNIT")=PSSRGUNB,PSSDBFAL=1
 .I $G(PSSRGUN2)="" D
 ..I $G(PSSRGUNA)'="" S PSSDBAR("AMN")=PSSRGNM2,PSSDBAR("UNIT")=PSSRGUNA,PSSDBFAL=1
 I $G(PSSRGUN1)'="" S PSSRGUNA=$$UNIT^PSSDSAPI(PSSRGUN1)
 I $G(PSSRGUN2)'="" S PSSRGUNB=$$UNIT^PSSDSAPI(PSSRGUN2)
 I $G(PSSRGUN1)'="",$G(PSSRGUN2)'="" D  Q
 .I $G(PSSRGUNA)'="",PSSRGUNA=$G(PSSRGUNB) S PSSDBAR("AMN")=PSSRGNM2,PSSDBAR("UNIT")=PSSRGUNA,PSSDBFAL=1
 I $G(PSSRGUN1)="" D
 .I $G(PSSRGUNB)'="" S PSSDBAR("AMN")=PSSRGNM2,PSSDBAR("UNIT")=PSSRGUNB,PSSDBFAL=1
 I $G(PSSRGUN2)="" D
 .I $G(PSSRGUNA)'="" S PSSDBAR("AMN")=PSSRGNM2,PSSDBAR("UNIT")=PSSRGUNA,PSSDBFAL=1
 Q
 ;
 ;
MLTOK(PSSUTM1,PSSUTM3) ;If multi ingredient, and all ingredients in VA Product have a Unit and it matches the unit from the order
 N DA,PSSUTMCT,PSSUTMLP,PSSUTMUX,PSSUTMAR,PSSUTMQT,PSSUTMUZ
 S PSSUTMQT=0
 S PSSUTMCT=$$PSJING^PSNAPIS(PSSUTM1,PSSUTM3,.PSSUTMAR) I $G(PSSUTMCT)'>1 Q 1
 F PSSUTMLP=0:0 S PSSUTMLP=$O(PSSUTMAR(PSSUTMLP)) Q:'PSSUTMLP!(PSSUTMQT)  D
 .S PSSUTMUX=$P(PSSUTMAR(PSSUTMLP),"^",4) I PSSUTMUX="" S PSSUTMQT=1 Q
 .S PSSUTMUZ=$$UNITD^PSSDSAPI(PSSUTMUX) I PSSUTMUZ="" S PSSUTMQT=1 Q
 .I PSSUTMUZ'=PSSDBAR("UNIT") S PSSUTMQT=1
 Q PSSUTMQT
 ;
 ;
UPCPRS ;Update CPRS global
 I '$D(^TMP($J,PSSDBASF)) Q
 N PSSCPC5,PSSCPC6,PSSCPC7,PSSCPC8,PSSCPC9,PSSCPCNM,PSSCPGL,PSSCPCND,PSSCPCNN,PSSCPCAR,PSSCPCNX,PSSCPCO1,PSSCPCO2,PSSCPCO3,PSSCPCG1,PSSCPCGN,PSSCPCWA,PSSCPBMR,PSSCPCAJ,PSSCPCAC,PSSCPCE1,PSSCPCE2,PSSCPCR1,PSSCPCR2
 N PSSCPCR3,PSSCPCR4,PSSCPCR5,PSSCPCR6
 K ^TMP($J,"PSSCPNEW") M ^TMP($J,"PSSCPNEW")=^TMP($J,PSSDBASF) K ^TMP($J,PSSDBASF)
 S PSSCPCGL="PSSCPNEW"
 ;
 ;Errors - remove Dose subscript, and piece 27 check to to see any error should be kept
 S PSSCPC5="" F  S PSSCPC5=$O(PSSDBCAR(PSSCPC5)) Q:PSSCPC5=""  S PSSCPCWA=0 D:'$P(PSSDBCAR(PSSCPC5),"^",14)
 .S PSSCPCR1=0,(PSSCPCR2,PSSCPCR5)=1,PSSCPC6="" F  S PSSCPC6=$O(^TMP($J,PSSCPCGL,"OUT","DOSE","ERROR",PSSCPC5,PSSCPC6)) Q:PSSCPC6=""  S PSSCPCNM=$P(PSSCPC5,";",4) D
 ..S PSSCPCE1=$G(^TMP($J,PSSCPCGL,"OUT","DOSE","ERROR",PSSCPC5,PSSCPC6,"MSG"))
 ..S PSSCPCE2=$G(^TMP($J,PSSCPCGL,"OUT","DOSE","ERROR",PSSCPC5,PSSCPC6,"TEXT"))
 ..I $G(^TMP($J,PSSCPCGL,"OUT","DOSE","ERROR",PSSCPC5,PSSCPC6,"WARN"))="Warning",'$P(PSSCPC5,";",5) S $P(PSSDBCAR(PSSCPC5),"^",13)="",PSSCPCWA=1 D  Q
 ...S ^TMP($J,PSSDBASF,"OUT","CHECK",PSSCPCNM,PSSCPC5,1,"ATYPE")="DOSE^EXCEPTION"
 ...S ^TMP($J,PSSDBASF,"OUT","CHECK",PSSCPCNM,PSSCPC5,1,"MSG",1)=PSSCPCE1_$S($E(PSSCPCE1,$L(PSSCPCE1))'=":":":",1:"")_" "_PSSCPCE2
 ..I '$P(PSSDBCAR(PSSCPC5),"^",27) Q
 ..I '$O(^TMP($J,PSSCPCGL,"OUT","EXCEPTIONS","DOSE",PSSCPC5,""))!(PSSCPCR1) D  Q
 ...S PSSCPCR1=1
 ...S ^TMP($J,PSSCPCGL,"OUT","EXCEPTIONS","DOSE",PSSCPC5,PSSCPCR2)=PSSCPCE1_$S($E(PSSCPCE1,$L(PSSCPCE1))'=".":".",1:"")_" "_PSSCPCE2,PSSCPCR2=PSSCPCR2+1
 ..I PSSCPCR5=1 K PSSCPCR3 S PSSCPCR4="" F  S PSSCPCR4=$O(^TMP($J,PSSCPCGL,"OUT","EXCEPTIONS","DOSE",PSSCPC5,PSSCPCR4)) Q:PSSCPCR4=""  D
 ...S PSSCPCR3(PSSCPCR4)=^TMP($J,PSSCPCGL,"OUT","EXCEPTIONS","DOSE",PSSCPC5,PSSCPCR4) K ^TMP($J,PSSCPCGL,"OUT","EXCEPTIONS","DOSE",PSSCPC5,PSSCPCR4)
 ..S ^TMP($J,PSSCPCGL,"OUT","EXCEPTIONS","DOSE",PSSCPC5,PSSCPCR5)=PSSCPCE1_$S($E(PSSCPCE1,$L(PSSCPCE1))'=".":".",1:"")_" "_PSSCPCE2,PSSCPCR5=PSSCPCR5+1
 .I $O(PSSCPCR3("")) S PSSCPCR6="" F  S PSSCPCR6=$O(PSSCPCR3(PSSCPCR6)) Q:PSSCPCR6=""  D
 ..S ^TMP($J,PSSCPCGL,"OUT","EXCEPTIONS","DOSE",PSSCPC5,PSSCPCR5)=PSSCPCR3(PSSCPCR6),PSSCPCR5=PSSCPCR5+1
 .;
 .Q:PSSCPCWA  ;Quit if warning
 .S PSSCPCAR(PSSCPC5)=1 S PSSCPCNM=$P(PSSCPC5,";",4) I $P(PSSDBCAR(PSSCPC5),"^",27) D
 ..S PSSCPC6="" F  S PSSCPC6=$O(^TMP($J,PSSCPCGL,"OUT","EXCEPTIONS","DOSE",PSSCPC5,PSSCPC6)) Q:PSSCPC6=""  D
 ...S PSSCPCND=$G(^TMP($J,PSSCPCGL,"OUT","EXCEPTIONS","DOSE",PSSCPC5,PSSCPC6)) Q:PSSCPCND=""
 ...S ^TMP($J,PSSDBASF,"OUT","CHECK",PSSCPCNM,PSSCPC5,PSSCPCAR(PSSCPC5),"ATYPE")="DOSE^EXCEPTION"
 ...S ^TMP($J,PSSDBASF,"OUT","CHECK",PSSCPCNM,PSSCPC5,PSSCPCAR(PSSCPC5),"MSG",1)=PSSCPCND,PSSCPCAR(PSSCPC5)=PSSCPCAR(PSSCPC5)+1
 .;Set generic exception if needed
 .I '$P(PSSDBCAR(PSSCPC5),"^",27),('$G(PSSENHK(PSSCPC5))!('$G(PSSENHKZ(PSSCPC5)))) D
 ..;I $P(PSSDBCAR(PSSCPC5),"^",29) S $P(PSSDBCAR(PSSCPC5),"^",4)=0 ;need this to show Daily Dose error
 ..I '$D(PSSDSDPL(PSSCPC5))!($P(PSSDBCAR(PSSCPC5),"^",4)&('$P(PSSDBCAR(PSSCPC5),"^",13))) Q
 ..S PSSCPCG1="" I '$P(PSSDBCAR(PSSCPC5),"^",13),'$P(PSSDBCAR(PSSCPC5),"^",4) S PSSCPCG1="Max Daily Dose Check"
 ..I '$P(PSSDBCAR(PSSCPC5),"^",13),PSSCPCG1="" Q
 ..I PSSCPCG1'="",$P(PSSDBCAR(PSSCPC5),"^",15)!($P(PSSDBCAR(PSSCPC5),"^",16))!($P(PSSCPC5,";",5)) K PSSCPCG1 Q
 ..I PSSCPCG1="" S PSSCPCG1=$S('$P(PSSDBCAR(PSSCPC5),"^",15)&('$P(PSSDBCAR(PSSCPC5),"^",16)):"Dosing Checks",1:"Maximum Single Dose Check")
 ..S PSSCPBMR=$S($P(PSSDBCAR(PSSCPC5),"^",31):$E($P(PSSDBCAR(PSSCPC5),"^",32),1,($L($P(PSSDBCAR(PSSCPC5),"^",32))-2)),1:"")_$S($P(PSSDBCAR(PSSCPC5),"^",16):" (Dose="_$G(PSSDSDPL(PSSCPC5))_")",1:"")
 ..S PSSCPCGN=PSSCPCG1_" could not be done for Drug: "_$P(PSSDBCAR(PSSCPC5),"^",2)_PSSCPBMR
 ..S PSSCPCGN=PSSCPCGN_", please complete a manual check for appropriate Dosing."
 ..S ^TMP($J,PSSDBASF,"OUT","CHECK",PSSCPCNM,PSSCPC5,PSSCPCAR(PSSCPC5),"ATYPE")="DOSE^EXCEPTION"
 ..S ^TMP($J,PSSDBASF,"OUT","CHECK",PSSCPCNM,PSSCPC5,PSSCPCAR(PSSCPC5),"MSG",1)=PSSCPCGN,PSSCPCAR(PSSCPC5)=PSSCPCAR(PSSCPC5)+1
 .;
 .K PSSCPCAJ S PSSCPCO2="",(PSSCPCO3,PSSCPCAC)=0 S PSSCPC6="" F  S PSSCPC6=$O(^TMP($J,PSSCPCGL,"OUT","DOSE",PSSCPC5,PSSCPC6)) Q:PSSCPC6=""  D
 ..D PEROR S PSSCPCNM=$P(PSSCPC5,";",4) S PSSCPC7="" F  S PSSCPC7=$O(^TMP($J,PSSCPCGL,"OUT","DOSE",PSSCPC5,PSSCPC6,PSSCPC7)) Q:PSSCPC7=""  D
 ...I +PSSCPC7=2,$P(PSSDBCAR(PSSCPC5),"^",15)!($P(PSSDBCAR(PSSCPC5),"^",16))!($P(PSSCPC5,";",5)) Q  ;2.1 added the conditions
 ...S PSSCPC8="" F  S PSSCPC8=$O(^TMP($J,PSSCPCGL,"OUT","DOSE",PSSCPC5,PSSCPC6,PSSCPC7,"MESSAGE",PSSCPC8)) Q:PSSCPC8=""  D
 ....I PSSCPCO2'="" D
 .....S ^TMP($J,PSSDBASF,"OUT","CHECK",PSSCPCNM,PSSCPC5,PSSCPCAR(PSSCPC5),"ATYPE")="DOSE^"_$S(+PSSCPC7=1:"SINGLE",+PSSCPC7=2:"DAILY",1:"GENERAL")
 .....S ^TMP($J,PSSDBASF,"OUT","CHECK",PSSCPCNM,PSSCPC5,PSSCPCAR(PSSCPC5),"MSG",1)=PSSCPCO2
 ....I +PSSCPC7'=3 S PSSCPCNN=$S(PSSCPCO3:2,1:1) D:$G(PSSDBADJ(PSSCPC5))'=""&($G(PSSCPCAJ(PSSCPC5))="") SADJ D  Q
 .....S PSSCPCND=^TMP($J,PSSCPCGL,"OUT","DOSE",PSSCPC5,PSSCPC6,PSSCPC7,"MESSAGE",PSSCPC8) I PSSCPCAC S PSSCPCND=PSSCPCND_PSSCPCAJ(PSSCPC5)
 .....I PSSCPCNN=1 S ^TMP($J,PSSDBASF,"OUT","CHECK",PSSCPCNM,PSSCPC5,PSSCPCAR(PSSCPC5),"ATYPE")="DOSE^"_$S(+PSSCPC7=1:"SINGLE",1:"DAILY")
 .....S ^TMP($J,PSSDBASF,"OUT","CHECK",PSSCPCNM,PSSCPC5,PSSCPCAR(PSSCPC5),"MSG",PSSCPCNN)=PSSCPCND,PSSCPCAR(PSSCPC5)=PSSCPCAR(PSSCPC5)+1
 ....S PSSCPCNN=$S(PSSCPCO3:1,1:0),PSSCPCAA=$S(PSSCPCNN:2,1:1)
 ....S PSSCPC9="" F  S PSSCPC9=$O(^TMP($J,PSSCPCGL,"OUT","DOSE",PSSCPC5,PSSCPC6,PSSCPC7,"MESSAGE",PSSCPC8,PSSCPC9)) Q:PSSCPC9=""  D
 .....I 'PSSCPCNN S ^TMP($J,PSSDBASF,"OUT","CHECK",PSSCPCNM,PSSCPC5,PSSCPCAR(PSSCPC5),"ATYPE")="DOSE^GENERAL"
 .....S PSSCPCND=^TMP($J,PSSCPCGL,"OUT","DOSE",PSSCPC5,PSSCPC6,PSSCPC7,"MESSAGE",PSSCPC8,PSSCPC9)
 .....S ^TMP($J,PSSDBASF,"OUT","CHECK",PSSCPCNM,PSSCPC5,PSSCPCAR(PSSCPC5),"MSG",PSSCPCAA)=PSSCPCND,PSSCPCAA=PSSCPCAA+1,PSSCPCAR(PSSCPC5)=PSSCPCAR(PSSCPC5)+1
 .I $P(PSSDBCAR(PSSCPC5),"^",29) D CFREQ
 K ^TMP($J,PSSCPCGL)
 Q
 ;
 ;
CFREQ ;Add customized Frequency
 I $P(PSSDBCAR(PSSCPC5),"^",15)!($P(PSSDBCAR(PSSCPC5),"^",16))!($P(PSSCPC5,";",5)) Q
 S ^TMP($J,PSSDBASF,"OUT","CHECK",PSSCPCNM,PSSCPC5,PSSCPCAR(PSSCPC5),"ATYPE")="DOSE^INFORMATIONAL"
 S ^TMP($J,PSSDBASF,"OUT","CHECK",PSSCPCNM,PSSCPC5,PSSCPCAR(PSSCPC5),"MSG",1)=$G(^TMP($J,PSSDBASE,"OUT","DOSE",PSSCPC5,$P(PSSDBCAR(PSSCPC5),"^",2),"FREQ","FREQUENCYCUSTOMMESSAGE",$P(PSSDBCAR(PSSCPC5),"^",3)))
 Q
 ;
 ;
PEROR ;Per Orifice check
 N PSSCPCO9
 S PSSCPCO9=$P(PSSDBCAR(PSSCPC5),"^",9) I PSSCPCO9="OTIC"!(PSSCPCO9="OPHTHALMIC")!(PSSCPCO9="INTRANASAL") D
 .S PSSCPCO2="Dosing Information provided is PER "_$S(PSSCPCO9="OTIC":"EAR:",PSSCPCO9="OPHTHALMIC":"EYE:",1:"NOSTRIL:"),PSSCPCO3=1
 Q
 ;
 ;
SADJ ;Parse out Adjusted Frequency message
 N PSSSDADJ
 S PSSCPCAC=1
 S PSSSDADJ=$F(PSSDBADJ(PSSCPC5),"(") I 'PSSSDADJ S PSSCPCAJ(PSSCPC5)=" "_PSSDBADJ(PSSCPC5) Q
 S PSSCPCAJ(PSSCPC5)=" "_$E(PSSDBADJ(PSSCPC5),(PSSSDADJ-1),$L(PSSDBADJ(PSSCPC5))-1)
 Q
 ;
 ;
PTH ;Local Dosage with parenthesis, extract data from before and within
 N PSSPTH1,PSSPTH2,PSSPTH3,PSSPTH4,PSSPTHL,PSSPTHA,PSSPTHC,PSSPTHF,PSSPTHD,PSSPTHS1,PSSPTHS2,PSSPTHS3,PSSPTHS4
 K PSSDSLC1 S (PSSPTH1,PSSPTH2,PSSPTH3,PSSPTH4,PSSPTHF)=0,PSSPTHD=PSSDSLCL
 S PSSPTHL=$L(PSSPTHD)
 F PSSPTHA=1:1:PSSPTHL Q:PSSPTHF  S PSSPTHC=$E(PSSPTHD,PSSPTHA) D
 .I PSSPTHC'="(",PSSPTHC'=")" Q
 .I PSSPTHC="(" S PSSPTH1=PSSPTHA,PSSPTH3=PSSPTH3+1 S:PSSPTH3>1 PSSPTHF=1 Q
 .S PSSPTH2=PSSPTHA,PSSPTH4=PSSPTH4+1 S:PSSPTH4>1 PSSPTHF=1 Q
 Q:PSSPTHF  ;Quit if more than 1 of either parenthesis
 Q:'PSSPTH3!('PSSPTH4)  ;Quit if either parenthesis is missing
 Q:PSSPTH1'<PSSPTH2  ;Quit if left paren is not before right paren
 I PSSPTH1>1 S PSSPTHS1="" D  I PSSPTHS1'="" S PSSDSLC1(2)=PSSPTHS1,PSSDSLCT=2
 .S PSSPTHS1=$E(PSSPTHD,1,(PSSPTH1-1)),PSSPTHS2=$L(PSSPTHS1),PSSPTHS4=0
 .F PSSPTHS3=PSSPTHS2:-1:1 I $E(PSSPTHS1,PSSPTHS3)'=" " S PSSPTHS4=PSSPTHS3 Q
 .Q:PSSPTHS4=PSSPTHS2
 .I 'PSSPTHS4 S PSSPTHS1="" Q
 .S PSSPTHS1=$E(PSSPTHS1,1,PSSPTHS4)
 I (PSSPTH1+1)'<PSSPTH2 D:PSSDSLCT  Q
 .S PSSDSLC1(1)=PSSDSLC1(2),PSSDSLCT=1 K PSSDSLC1(2)
 S PSSDSLC1(1)=$E(PSSPTHD,(PSSPTH1+1),(PSSPTH2-1)) I 'PSSDSLCT S PSSDSLCT=1
 Q
 ;
 ;
RTEXT(PSSBAMRT,PSSBSPC) ;Set Reason Header
 N PSSBSPCS S PSSBSPCS=$S(PSSBSPC:"  ",1:"")
 S PSSDWRSN=$S('$P(PSSDBCAR(PSSBAMRT),"^",31):PSSBSPCS_"Reason(s): ",1:PSSBSPCS_"Reason(s)"_$P(PSSDBCAR(PSSBAMRT),"^",32))
 Q
 ;
 ;
ORDFREQ(PSSDADF) ; -- in 2.1 get order frequency by converting FDB frequency patterns -- called from PSSHRQ23
 ;PSSDADF - Frequency # or Pattern
 ;
 ;Return: Order Frequency or 0
 ;
 N PSSDADL,PSSDADN,PSSDADTM,PSSDADS
 ; -- check for missing variable, exit if not defined
 I $G(PSSDADF)']"" Q 0
 ; -- check for number
 I $G(PSSDADF) Q $G(PSSDADF)
 ; -- every other day
 I PSSDADF="QOD" Q .5
 ; -- set PSSDADL=Frequency Length, exit if not equal to 3 or 4
 S PSSDADL=$L(PSSDADF) I PSSDADL'=3,PSSDADL'=4 Q 0
 ; -- set PSSDADS=Action associated with frequency Q=every, X=times
 S PSSDADS=$E(PSSDADF)
 ; -- check action associated with frequency, exit if not "Q" or "X"
 I PSSDADS'="Q",PSSDADS'="X" Q 0
 ; -- set PSSDADN=Frequency Number
 S PSSDADN=$E(PSSDADF,2,$L(PSSDADF)-1)
 ; -- check if PSSDADN is numeric, exit if it is not
 I PSSDADN'?.N Q 0
 ; -- set PSSDADTM=period of time associated with frequency H=hour, D=day, W=week, L=month
 S PSSDADTM=$E(PSSDADF,PSSDADL)
 ; -- calculate order frequency every # hour(s)
 I PSSDADS="Q",PSSDADTM="H" Q 24/PSSDADN
 ; -- calculate order frequency every # days(s)
 I PSSDADS="Q",PSSDADTM="D" Q 1/PSSDADN
 ; -- calculate order frequency every # week(s)
 I PSSDADS="Q",PSSDADTM="W" Q 1/(PSSDADN*7)
 ; -- calculate order frequency every # month(s)
 I PSSDADS="Q",PSSDADTM="L" Q 1/(PSSDADN*30)
 ; -- calculate order frequency # times per day
 I PSSDADS="X",PSSDADTM="D" Q PSSDADN
 ; -- calculate order frequency # times per week
 I PSSDADS="X",PSSDADTM="W" Q PSSDADN/7
 ; -- calculate order frequency # times per month
 I PSSDADS="X",PSSDADTM="L" Q PSSDADN/30
 Q 0
 ;
 ;
ROUNDNUM(X) ; -- in 2.1 if number is < or = 1, round to 4 decimals otherwise no decimals -- called from PSSHRQ23 
 ;X - Number
 ;
 ;Return: Rounded Number or 0
 N N,ND
 ; -- check for missing variable, exit if not defined
 I $G(X)'>0 Q 0
 ; -- get number of decimals
 S ND=$L($P(+X,".",2))
 ; -- set number Of decimals
 S N=$S(+X>1:0,1:4)
 ; -- if number is zero, round up until number is nolonger zero 
 I $J(+X,"",N)'>0 D
 . F N=1:1:ND Q:$J(X,"",N)>0
 Q +$J(X,"",N)
 ;
 ;
PRNSCHD(PSSSCHD) ;If 'PRN' appended to the schedule, return the schedule with 'PRN' remove
 NEW PSSXL,PSSXSCHD,PSSXSCHN
 I $G(PSSSCHD)="" Q ""
 I $D(^PS(51.1,"APPSJ",PSSSCHD)) Q PSSSCHD
 S PSSXL=$L(PSSSCHD)
 I $E(PSSSCHD,(PSSXL-2),PSSXL)="PRN" D  ;Check name cross-index
 . S PSSXSCHD=$E(PSSSCHD,1,(PSSXL-4))
 . I (PSSXSCHD'=""),(PSSXSCHD'?." ") S:$D(^PS(51.1,"APPSJ",PSSXSCHD)) PSSSCHD=PSSXSCHD
 I $E(PSSSCHD,(PSSXL-2),PSSXL)="PRN" D  ;Check Old Schedule Name(s) cross-index PSS*1*231
 . S PSSXSCHD=$E(PSSSCHD,1,(PSSXL-4))
 . I (PSSXSCHD'=""),(PSSXSCHD'?." "),$D(^PS(51.1,"D",PSSXSCHD)) S PSSXSCHN=$O(^PS(51.1,"D",PSSXSCHD,0)),PSSSCHD=$P($G(^PS(51.1,PSSXSCHN,0)),U,1)
 Q PSSSCHD
 ;
 ;
PRNMI(PSSMI) ;If 'PRN' appended to the Med instruction, return the MI with 'PRN' remove
 NEW PSSXL,PSSXMI
 I $G(PSSMI)="" Q ""
 I $D(^PS(51,"B",PSSMI)) Q PSSMI
 S PSSXL=$L(PSSMI)
 I $E(PSSMI,(PSSXL-2),PSSXL)="PRN" D
 . S PSSXMI=$E(PSSMI,1,(PSSXL-4))
 . I (PSSXMI'=""),(PSSXMI'?." ") S:$D(^PS(51,"B",PSSXMI)) PSSMI=PSSXMI
 Q PSSMI