Presentation is loading. Please wait.

Presentation is loading. Please wait.

Procedures Organized by Farrokh Alemi, Ph.D. Narrated by Yara Alemi

Similar presentations


Presentation on theme: "Procedures Organized by Farrokh Alemi, Ph.D. Narrated by Yara Alemi"— Presentation transcript:

1 Procedures Organized by Farrokh Alemi, Ph.D. Narrated by Yara Alemi
This section provides a brief introduction to receiver Operating Curves, a tool used to test the predictive accuracy of models. This brief presentation was organized by Dr. Alemi.

2 Procedure Syntax Again
Procedures are used when portion of a code must be repeatedly executed. A procedure is a subprogram or module that performs a particular data manipulation task. Procedures allow ‘modular design‘ of the SQL code. A subprogram can be called repeatedly within the code thus reducing repeated code.

3 CREATE PROCEDURE DROP PROCEDURE
Procedures are created with the CREATE PROCEDURE statement. It is stored in the database and can be deleted with the DROP PROCEDURE statement.

4 Declarative Executable Exceptions
Each procedure has a name, and may also have a parameter list. It also has the following three parts: declarative, executable, and exceptions. Declarative Part is an optional part. It contains declarations of types, cursors, constants, variables, exceptions, and nested subprograms. These items are local to the procedure and cease to exist when it completes execution.

5 Declarative Executable Exceptions
The executable Part is a mandatory part and contains statements that perform the designated action.

6 Declarative Executable Exceptions
The Exception-handling part is an optional part. It contains the code that handles errors during run time.

7 CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > END; A procedure is created or replaced with the CREATE statement.

8 CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > END; The procedure-name obviously specifies the name of the procedure. It must not be a reserved word and should not contain space.

9 CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > END; The optional parameter list contains name, mode and types of the parameters. IN represents the value that will be passed from outside, when the procedure is called. It is a read-only parameter. Inside the subprogram, an IN parameter acts like a constant. It cannot be assigned a value. You can pass a constant, literal, initialized variable, or expression as an IN parameter. 

10 CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > END; OUT represents the parameter that will be used to return a value to the calling program. Inside the procedure, an OUT parameter acts like a variable. You can change its value and reference the value after assigning it.

11 CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > END; An IN OUT parameter type passes an initial value to a subprogram and returns an updated value to the caller. It can be assigned a value in the calling program and the value can be updated in the procedure.

12 CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > END; Type refers to variable type such as integer, float, variable character and so on.

13 CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > END; The AS keyword is used instead of the IS keyword for creating a standalone procedure. Within a calling program, the EXECUTE command runs the standalone procedure.

14 CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > END; procedure-body contains the executable part. This is where the input variables are modified to produce the output variables

15 CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > END; The last line of a procedure should be the END command.

16 Example Procedure Again
We demonstrate the use of procedure with one created to calculate likelihood ratio.

17 -- DROP PROCEDURE Likelihood_Ratio;
CREATE PROCEDURE Likelihood_Ratio float ) AS BEGIN SELECT MAX(CASE WHEN Cast(ICD9 as then 1. Else 0. END) AS Dx , MAX(CASE WHEN AgeAtDeath is not null then 1. Else 0. END) AS Dead INTO #temp1 FROM [AgeDx].[dbo].[final] GROUP BY id CASE WHEN SUM(Dead)is null then 1/SUM(1-Dead) WHEN SUM(1-Dead)<1 then SUM(Dead)+1 Else END AS [Likelihood Ratio] FROM #temp1 END In this procedure we calculate the likelihood ratio associated with a specific diagnosis.

18 -- DROP PROCEDURE Likelihood_Ratio;
CREATE PROCEDURE Likelihood_Ratio float ) AS BEGIN SELECT MAX(CASE WHEN Cast(ICD9 as then 1. Else 0. END) AS Dx , MAX(CASE WHEN AgeAtDeath is not null then 1. Else 0. END) AS Dead INTO #temp1 FROM [AgeDx].[dbo].[final] GROUP BY id CASE WHEN SUM(Dead)is null then 1/SUM(1-Dead) WHEN SUM(1-Dead)<1 then SUM(Dead)+1 Else END AS [Likelihood Ratio] FROM #temp1 END The first line is commented out and is used only when we wish to drop the previous draft of the procedure from the database.

19 -- DROP PROCEDURE Likelihood_Ratio;
CREATE PROCEDURE Likelihood_Ratio float ) AS BEGIN SELECT MAX(CASE WHEN Cast(ICD9 as then 1. Else 0. END) AS Dx , MAX(CASE WHEN AgeAtDeath is not null then 1. Else 0. END) AS Dead INTO #temp1 FROM [AgeDx].[dbo].[final] GROUP BY id CASE WHEN SUM(Dead)is null then 1/SUM(1-Dead) WHEN SUM(1-Dead)<1 then SUM(Dead)+1 Else END AS [Likelihood Ratio] FROM #temp1 END This line gives the name of the procedure and the types of variables that are used as input in the procedure. Note that the name of these variables could be different from the names used to call the procedure and the computer will set the calling variable set to the procedure variable set in the order listed. The seven letter-long variable called at DX contains the diagnosis code. The procedure will calculate the likelihood ratio associated with this diagnosis code.

20 -- DROP PROCEDURE Likelihood_Ratio;
CREATE PROCEDURE Likelihood_Ratio float ) AS BEGIN SELECT MAX(CASE WHEN Cast(ICD9 as then 1. Else 0. END) AS Dx , MAX(CASE WHEN AgeAtDeath is not null then 1. Else 0. END) AS Dead INTO #temp1 FROM [AgeDx].[dbo].[final] GROUP BY id CASE WHEN SUM(Dead)is null then 1/SUM(1-Dead) WHEN SUM(1-Dead)<1 then SUM(Dead)+1 Else END AS [Likelihood Ratio] FROM #temp1 END This section is the execution code. During this section computer calculates a likelihood ratio for the diagnoses code used to call the procedure.

21 -- DROP PROCEDURE Likelihood_Ratio;
CREATE PROCEDURE Likelihood_Ratio float ) AS BEGIN SELECT MAX(CASE WHEN Cast(ICD9 as then 1. Else 0. END) AS Dx , MAX(CASE WHEN AgeAtDeath is not null then 1. Else 0. END) AS Dead INTO #temp1 FROM [AgeDx].[dbo].[final] GROUP BY id CASE WHEN SUM(Dead)is null then 1/SUM(1-Dead) WHEN SUM(1-Dead)<1 then SUM(Dead)+1 Else END AS [Likelihood Ratio] FROM #temp1 END It reads the data from premanant table “final” in “agedx” database. For each unique person, it calculates if the patient has the diagnosis or does not. It also calculates if the patient is dead or alive. It puts these data in temporary file 1. Notice that repeated calls to the procedure does not require us to drop the temporary file as each call causes all temporary files to drop.

22 -- DROP PROCEDURE Likelihood_Ratio;
CREATE PROCEDURE Likelihood_Ratio float ) AS BEGIN SELECT MAX(CASE WHEN Cast(ICD9 as then 1. Else 0. END) AS Dx , MAX(CASE WHEN AgeAtDeath is not null then 1. Else 0. END) AS Dead INTO #temp1 FROM [AgeDx].[dbo].[final] GROUP BY id CASE WHEN SUM(Dead)is null then 1/SUM(1-Dead) WHEN SUM(1-Dead)<1 then SUM(Dead)+1 Else END AS [Likelihood Ratio] FROM #temp1 END Next the likelihood ratio is calculated for patients who have the diagnosis. Separate calculations are done for situations where no one with the diagnosis is dead, where everyone with the diagnosis is dead, and where some of the patients with the diagnosis are dead and some are alive.

23 Execution of Procedure Again
To run a procedure one must call it using the EXECUTE command

24 EXECUTE procedure_name [parameter_name [, ...]]
The syntax is the reserve word execute followed by the procedure name followed with procedure parameters separated by commas.

25 DECLARE @Diag varchar(7), @TotalAlive float, @TotalDead float
= 'I789.00' = (SELECT CAST(COUNT(distinct ID) as FLOAT) FROM [AgeDx].[dbo].[final] WHERE AgeAtDeath is null) = (SELECT CAST(COUNT(distinct ID) as FLOAT) FROM [AgeDx].[dbo].[final] WHERE AgeAtDeath is not null) @TotalDead This code calls the previously described likelihood ratio procedure.

26 DECLARE @Diag varchar(7), @TotalAlive float, @TotalDead float
= 'I789.00' = (SELECT CAST(COUNT(distinct ID) as FLOAT) FROM [AgeDx].[dbo].[final] WHERE AgeAtDeath is null) = (SELECT CAST(COUNT(distinct ID) as FLOAT) FROM [AgeDx].[dbo].[final] WHERE AgeAtDeath is not null) @TotalDead First the various parameters are declared and calculated. Here the diagnosis code is set and the total patients alive and dead are calculated from the data.

27 DECLARE @Diag varchar(7), @TotalAlive float, @TotalDead float
= 'I789.00' = (SELECT CAST(COUNT(distinct ID) as FLOAT) FROM [AgeDx].[dbo].[final] WHERE AgeAtDeath is null) = (SELECT CAST(COUNT(distinct ID) as FLOAT) FROM [AgeDx].[dbo].[final] WHERE AgeAtDeath is not null) @TotalDead The execute command tells the computer to run the procedure called likelihood ratio and to pass to this procedure three pieces of data, a constant containing the diagnosis code in text, and two other floating numbers containing the total number of patients alive or dead.

28 The results of the executing the procedure is the calculated likelihood ratio that can be passed back to the calling program or stored in a permanent file using the APPEND command.

29 One can repeatedly call and execute a procedure
One can repeatedly call and execute a procedure. This provides an important advantage to coding as it allows portion of the code to be reused.

30 Procedures can stream line repeated code


Download ppt "Procedures Organized by Farrokh Alemi, Ph.D. Narrated by Yara Alemi"

Similar presentations


Ads by Google