Skip to content

Commit

Permalink
Fixed demo procedure to grab only patient_dimesion ontology values
Browse files Browse the repository at this point in the history
The union in the demo procedure was finding both patient_dimension
entries as well as concept_dimension entries because the where clauses
for the sex records was to broad. Changed like value to have a ending
backslash to avoid the concept_dimension records which are NOT 'IN's.
  • Loading branch information
kembree1 committed Jul 27, 2017
1 parent 98a3d80 commit 31762fc
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 12 deletions.
177 changes: 177 additions & 0 deletions MSSQL/OMOPDemo_on_PMI.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
USE [PMI]
GO

/****** Object: StoredProcedure [dbo].[OMOPdemographics] Script Date: 7/18/2017 10:47:29 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

create procedure [dbo].[OMOPdemographics] as

DECLARE @sqltext NVARCHAR(4000);
DECLARE @batchid numeric
declare getsql cursor local for
--1 -- S,R,NH
select 'insert into person(gender_source_value,race_source_value,ethnicity_source_value,person_id,year_of_birth,month_of_birth,day_of_birth,birth_datetime,gender_concept_id,ethnicity_concept_id,race_concept_id) '+ --person(raw_sex,PATID, BIRTH_DATE, BIRTH_TIME,SEX, HISPANIC, RACE)
' select p.sex_cd+'':'+sex.c_name+''',p.race_cd+'':'+race.c_name+''',p.race_cd+'':Unknown'',patient_num, '+
' year(birth_date), '+
' month(birth_date), '+
' day(birth_date), '+
' substring(convert(varchar,birth_date,20),12,5), '+
''''+sex.omop_basecode+''','+
'0,'+
''''+race.omop_basecode+''''+
' from i2b2patient p '+
' where lower(p.sex_cd) in ('+lower(sex.c_dimcode)+') '+
' and lower(p.race_cd) in ('+lower(race.c_dimcode)+') '+
' and lower(isnull(p.race_cd,''xx'')) not in (select lower(code) from omop_codelist where codetype=''HISPANIC'') '
from pcornet_demo race, pcornet_demo sex
where race.c_fullname like '\PCORI\DEMOGRAPHIC\RACE%'
and race.c_visualattributes like 'L%'
and sex.c_fullname like '\PCORI\DEMOGRAPHIC\SEX%'
and sex.c_visualattributes like 'L%'
union -- A - S,R,H
select 'insert into person(gender_source_value,race_source_value,ethnicity_source_value,person_id,year_of_birth,month_of_birth,day_of_birth,birth_datetime,gender_concept_id,ethnicity_concept_id,race_concept_id) '+
' select p.sex_cd+'':'+sex.c_name+''',p.race_cd+'':'+race.c_name+''',p.race_cd+'':'+hisp.c_name+''',patient_num, '+ --' select p.sex_cd+'':''+sex.c_name,p.race_cd+'':''+race.c_name,p.race_cd+'':''+hisp.c_name,patient_num, '+
' year(birth_date), '+
' month(birth_date), '+
' day(birth_date), '+
' substring(convert(varchar,birth_date,20),12,5), '+
''''+sex.omop_basecode+''','+
''''+hisp.omop_basecode+''','+
''''+race.omop_basecode+''''+
' from i2b2patient p '+
' where lower(p.sex_cd) in ('+lower(sex.c_dimcode)+') '+
' and lower(p.race_cd) in ('+lower(race.c_dimcode)+') '+
' and lower(isnull(p.race_cd,''xx'')) in (select lower(code) from omop_codelist where codetype=''RACE'') '+
' and lower(isnull(p.race_cd,''xx'')) in (select lower(code) from omop_codelist where codetype=''HISPANIC'') '
from pcornet_demo race, pcornet_demo hisp, pcornet_demo sex
where race.c_fullname like '\PCORI\DEMOGRAPHIC\RACE%'
and race.c_visualattributes like 'L%'
and hisp.c_fullname like '\PCORI\DEMOGRAPHIC\HISPANIC\Y%'
and hisp.c_visualattributes like 'L%'
and sex.c_fullname like '\PCORI\DEMOGRAPHIC\SEX%'
and sex.c_visualattributes like 'L%'
union --2 S, nR, nH
select 'insert into person(gender_source_value,race_source_value,ethnicity_source_value,person_id,year_of_birth,month_of_birth,day_of_birth,birth_datetime,gender_concept_id,ethnicity_concept_id,race_concept_id) '+
' select p.sex_cd+'':'+sex.c_name+''',p.race_cd+'':Unknown'',p.race_cd+'':Unknown'',patient_num, '+ --' select p.sex_cd,p.race_cd,p.race_cd,patient_num, '+
' year(birth_date), '+
' month(birth_date), '+
' day(birth_date), '+
' substring(convert(varchar,birth_date,20),12,5), '+
''''+sex.omop_basecode+''','+
'0,'+
'0'+
' from i2b2patient p '+
' where lower(isnull(p.sex_cd,''xx'')) in ('+lower(sex.c_dimcode)+') '+
' and lower(isnull(p.race_cd,''xx'')) not in (select lower(code) from omop_codelist where codetype=''RACE'') '+
' and lower(isnull(p.race_cd,''ni'')) not in (select lower(code) from omop_codelist where codetype=''HISPANIC'') '
from pcornet_demo sex
where sex.c_fullname like '\PCORI\DEMOGRAPHIC\SEX%'
and sex.c_visualattributes like 'L%'
union --3 -- nS,R, NH
select 'insert into person(gender_source_value,race_source_value,ethnicity_source_value,person_id,year_of_birth,month_of_birth,day_of_birth,birth_datetime,gender_concept_id,ethnicity_concept_id,race_concept_id) '+
' select p.sex_cd,p.race_cd+'':'+race.c_name+''',p.race_cd+'':Unknown'',patient_num, '+
' year(birth_date), '+
' month(birth_date), '+
' day(birth_date), '+
' substring(convert(varchar,birth_date,20),12,5), '+
'0,'+
'0,'+
''''+race.omop_basecode+''''+
' from i2b2patient p '+
' where lower(isnull(p.sex_cd,''xx'')) not in (select lower(code) from omop_codelist where codetype=''SEX'') '+
' and lower(p.race_cd) in ('+lower(race.c_dimcode)+') '+
' and lower(isnull(p.race_cd,''xx'')) not in (select lower(code) from omop_codelist where codetype=''HISPANIC'')'
from pcornet_demo race
where race.c_fullname like '\PCORI\DEMOGRAPHIC\RACE%'
and race.c_visualattributes like 'L%'
union --B -- nS,R, H
select 'insert into person(gender_source_value,race_source_value,ethnicity_source_value,person_id,year_of_birth,month_of_birth,day_of_birth,birth_datetime,gender_concept_id,ethnicity_concept_id,race_concept_id) '+
' select p.sex_cd,p.race_cd+'':'+race.c_name+''',p.race_cd+'':'+hisp.c_name+''',patient_num, '+
' year(birth_date), '+
' month(birth_date), '+
' day(birth_date), '+
' substring(convert(varchar,birth_date,20),12,5), '+
'0,'+
''''+hisp.omop_basecode+''','+
''''+race.omop_basecode+''''+
' from i2b2patient p '+
' where lower(isnull(p.sex_cd,''xx'')) not in (select lower(code) from omop_codelist where codetype=''SEX'') '+
' and lower(p.race_cd) in ('+lower(race.c_dimcode)+') '+
' and lower(isnull(p.race_cd,''xx'')) in (select lower(code) from omop_codelist where codetype=''RACE'') '+
' and lower(isnull(p.race_cd,''xx'')) in (select lower(code) from omop_codelist where codetype=''HISPANIC'')'
from pcornet_demo race,pcornet_demo hisp
where race.c_fullname like '\PCORI\DEMOGRAPHIC\RACE%'
and race.c_visualattributes like 'L%'
and hisp.c_fullname like '\PCORI\DEMOGRAPHIC\HISPANIC\Y%'
and hisp.c_visualattributes like 'L%'
union --4 -- S, NR, H
select 'insert into person(gender_source_value,race_source_value,ethnicity_source_value,person_id,year_of_birth,month_of_birth,day_of_birth,birth_datetime,gender_concept_id,ethnicity_concept_id,race_concept_id) '+
' select p.sex_cd+'':'+sex.c_name+''',p.race_cd+'':Unknown'',p.race_cd+'':Hispanic'',patient_num, '+
' year(birth_date), '+
' month(birth_date), '+
' day(birth_date), '+
' substring(convert(varchar,birth_date,20),12,5), '+
''''+sex.omop_basecode+''','+
'38003563,'+
'0'+
' from i2b2patient p '+
' where lower(isnull(p.sex_cd,''NI'')) in ('+lower(sex.c_dimcode)+') '+
' and lower(isnull(p.race_cd,''xx'')) not in (select lower(code) from omop_codelist where codetype=''RACE'') '+
' and lower(isnull(p.race_cd,''xx'')) in (select lower(code) from omop_codelist where codetype=''HISPANIC'') '
from pcornet_demo sex
where sex.c_fullname like '\PCORI\DEMOGRAPHIC\SEX%'
and sex.c_visualattributes like 'L%'
union --5 -- NS, NR, H
select 'insert into person(gender_source_value,race_source_value,ethnicity_source_value,person_id,year_of_birth,month_of_birth,day_of_birth,birth_datetime,gender_concept_id,ethnicity_concept_id,race_concept_id) '+
' select p.sex_cd,p.race_cd+'':Unknown'',p.race_cd+'':Hispanic'',patient_num, '+
' year(birth_date), '+
' month(birth_date), '+
' day(birth_date), '+
' substring(convert(varchar,birth_date,20),12,5), '+
'0,'+
'38003563,'+
'0'+
' from i2b2patient p '+
' where lower(isnull(p.sex_cd,''xx'')) not in (select lower(code) from omop_codelist where codetype=''SEX'') '+
' and lower(isnull(p.race_cd,''xx'')) not in (select lower(code) from omop_codelist where codetype=''RACE'') '+
' and lower(isnull(p.race_cd,''xx'')) in (select lower(code) from omop_codelist where codetype=''HISPANIC'')'
union --6 -- NS, NR, nH
select 'insert into person(gender_source_value,race_source_value,ethnicity_source_value,person_id,year_of_birth,month_of_birth,day_of_birth,birth_datetime,gender_concept_id,ethnicity_concept_id,race_concept_id) '+
' select p.sex_cd,p.race_cd+'':Unknown'',p.race_cd+'':Unknown'',patient_num, '+
' year(birth_date), '+
' month(birth_date), '+
' day(birth_date), '+
' substring(convert(varchar,birth_date,20),12,5), '+
'0,'+
'0,'+
'0'+
' from i2b2patient p '+
' where lower(isnull(p.sex_cd,''xx'')) not in (select lower(code) from omop_codelist where codetype=''SEX'') '+
' and lower(isnull(p.race_cd,''xx'')) not in (select lower(code) from omop_codelist where codetype=''HISPANIC'') '+
' and lower(isnull(p.race_cd,''xx'')) not in (select lower(code) from omop_codelist where codetype=''RACE'') '

begin
exec pcornet_popcodelist

set @batchid = 0
OPEN getsql;
FETCH NEXT FROM getsql INTO @sqltext;

WHILE @@FETCH_STATUS = 0
BEGIN
--print @sqltext
exec sp_executesql @sqltext
FETCH NEXT FROM getsql INTO @sqltext;

END

CLOSE getsql;
DEALLOCATE getsql;

end
GO

16 changes: 8 additions & 8 deletions MSSQL/OMOPLoader.sql
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,9 @@ declare getsql cursor local for
' and lower(p.race_cd) in ('+lower(race.c_dimcode)+') '+
' and lower(isnull(p.race_cd,''xx'')) not in (select lower(code) from omop_codelist where codetype=''HISPANIC'') '
from pcornet_demo race, pcornet_demo sex
where race.c_fullname like '\PCORI\DEMOGRAPHIC\RACE%'
where race.c_fullname like '\PCORI\DEMOGRAPHIC\RACE\%'
and race.c_visualattributes like 'L%'
and sex.c_fullname like '\PCORI\DEMOGRAPHIC\SEX%'
and sex.c_fullname like '\PCORI\DEMOGRAPHIC\SEX\%'
and sex.c_visualattributes like 'L%'
union -- A - S,R,H
select 'insert into person(gender_source_value,race_source_value,ethnicity_source_value,person_id,year_of_birth,month_of_birth,day_of_birth,birth_datetime,gender_concept_id,ethnicity_concept_id,race_concept_id) '+
Expand All @@ -345,11 +345,11 @@ select 'insert into person(gender_source_value,race_source_value,ethnicity_sourc
' and lower(isnull(p.race_cd,''xx'')) in (select lower(code) from omop_codelist where codetype=''RACE'') '+
' and lower(isnull(p.race_cd,''xx'')) in (select lower(code) from omop_codelist where codetype=''HISPANIC'') '
from pcornet_demo race, pcornet_demo hisp, pcornet_demo sex
where race.c_fullname like '\PCORI\DEMOGRAPHIC\RACE%'
where race.c_fullname like '\PCORI\DEMOGRAPHIC\RACE\%'
and race.c_visualattributes like 'L%'
and hisp.c_fullname like '\PCORI\DEMOGRAPHIC\HISPANIC\Y%'
and hisp.c_visualattributes like 'L%'
and sex.c_fullname like '\PCORI\DEMOGRAPHIC\SEX%'
and sex.c_fullname like '\PCORI\DEMOGRAPHIC\SEX\%'
and sex.c_visualattributes like 'L%'
union --2 S, nR, nH
select 'insert into person(gender_source_value,race_source_value,ethnicity_source_value,person_id,year_of_birth,month_of_birth,day_of_birth,birth_datetime,gender_concept_id,ethnicity_concept_id,race_concept_id) '+
Expand All @@ -366,7 +366,7 @@ union --2 S, nR, nH
' and lower(isnull(p.race_cd,''xx'')) not in (select lower(code) from omop_codelist where codetype=''RACE'') '+
' and lower(isnull(p.race_cd,''ni'')) not in (select lower(code) from omop_codelist where codetype=''HISPANIC'') '
from pcornet_demo sex
where sex.c_fullname like '\PCORI\DEMOGRAPHIC\SEX%'
where sex.c_fullname like '\PCORI\DEMOGRAPHIC\SEX\%'
and sex.c_visualattributes like 'L%'
union --3 -- nS,R, NH
select 'insert into person(gender_source_value,race_source_value,ethnicity_source_value,person_id,year_of_birth,month_of_birth,day_of_birth,birth_datetime,gender_concept_id,ethnicity_concept_id,race_concept_id) '+
Expand All @@ -383,7 +383,7 @@ union --3 -- nS,R, NH
' and lower(p.race_cd) in ('+lower(race.c_dimcode)+') '+
' and lower(isnull(p.race_cd,''xx'')) not in (select lower(code) from omop_codelist where codetype=''HISPANIC'')'
from pcornet_demo race
where race.c_fullname like '\PCORI\DEMOGRAPHIC\RACE%'
where race.c_fullname like '\PCORI\DEMOGRAPHIC\RACE\%'
and race.c_visualattributes like 'L%'
union --B -- nS,R, H
select 'insert into person(gender_source_value,race_source_value,ethnicity_source_value,person_id,year_of_birth,month_of_birth,day_of_birth,birth_datetime,gender_concept_id,ethnicity_concept_id,race_concept_id) '+
Expand All @@ -401,7 +401,7 @@ union --B -- nS,R, H
' and lower(isnull(p.race_cd,''xx'')) in (select lower(code) from omop_codelist where codetype=''RACE'') '+
' and lower(isnull(p.race_cd,''xx'')) in (select lower(code) from omop_codelist where codetype=''HISPANIC'')'
from pcornet_demo race,pcornet_demo hisp
where race.c_fullname like '\PCORI\DEMOGRAPHIC\RACE%'
where race.c_fullname like '\PCORI\DEMOGRAPHIC\RACE\%'
and race.c_visualattributes like 'L%'
and hisp.c_fullname like '\PCORI\DEMOGRAPHIC\HISPANIC\Y%'
and hisp.c_visualattributes like 'L%'
Expand All @@ -420,7 +420,7 @@ union --4 -- S, NR, H
' and lower(isnull(p.race_cd,''xx'')) not in (select lower(code) from omop_codelist where codetype=''RACE'') '+
' and lower(isnull(p.race_cd,''xx'')) in (select lower(code) from omop_codelist where codetype=''HISPANIC'') '
from pcornet_demo sex
where sex.c_fullname like '\PCORI\DEMOGRAPHIC\SEX%'
where sex.c_fullname like '\PCORI\DEMOGRAPHIC\SEX\%'
and sex.c_visualattributes like 'L%'
union --5 -- NS, NR, H
select 'insert into person(gender_source_value,race_source_value,ethnicity_source_value,person_id,year_of_birth,month_of_birth,day_of_birth,birth_datetime,gender_concept_id,ethnicity_concept_id,race_concept_id) '+
Expand Down
8 changes: 4 additions & 4 deletions MSSQL/run.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
-- All data from 1-1-2010 is transformed.
-- Jeff Klann, PhD, and Matthew Joss
--------------------------------------------------------------------------------------------------------------------------

use i2b2stub
drop table i2b2patient_list
GO

-- Make 100000000 number smaller for testing
select distinct top 100000000 f.patient_num into i2b2patient_list from i2b2fact f
select distinct top 100 f.patient_num into i2b2patient_list from i2b2fact f
inner join i2b2visit v on f.patient_num=v.patient_num
-- where f.start_date>='20100101' and v.start_date>='20100101'
GO
Expand All @@ -23,14 +23,14 @@ GO
drop view i2b2patient;
GO
-- Change to match your database name
create view i2b2patient as select * from PCORI_Mart..patient_dimension where patient_num in (select patient_num from i2b2patient_list)
create view i2b2patient as select * from i2b2demodata..patient_dimension where patient_num in (select patient_num from i2b2patient_list)
GO
drop synonym i2b2visit;
GO
drop view i2b2visit;
GO
-- Change to match your database name
create view i2b2visit as select * from PCORI_Mart..visit_dimension where (end_date is null or end_date<getdate());
create view i2b2visit as select * from i2b2demodata..visit_dimension where (end_date is null or end_date<getdate());
GO


Expand Down

0 comments on commit 31762fc

Please sign in to comment.