BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Banke
Pyrite | Level 9

Hi all,

i have two datasets: 

1. dataset1 = disease. it contains patients with disease (dx = 1) and no disease (dx = 0)

2. dataset 2 = medication. it contains patients taking medication for the disease (t2D = 1) and those not taking medications(t2D = 0)

 

i  need to get 1. patient with disease and taking med

2. patient with disease not taking med

3. patients without disease taking med

my code is too long,beacuse i had to create new datasets for each category of patients and used inner join.  I

/***QUESTION 1**/

/**TAKES EVERY PATIENT WITH THE MEDICATION THAT HAVE DIABETES**/
DATA DXMED; /*1,759,104*/
MERGE T2DMED2 (IN = A) /*1,759,104*/  T2DM2 (IN = B) /**/;
BY PATIENT_ID;
IF A =1 OR B = 1;
RUN;

PROC FREQ DATA = DXMED;
TABLES DX*T2D;
RUN;

**Q2**/

/**PATIENTS TREATED WITHOUT A DIAGNOSIS OF DIABETES**/


DATA MYHUMANA.T2DXXX; /**6,807,243**/ /*DISTINCT PATIENTS WITH NO T2D**/
SET MYHUMANA.T2DX; /*DISTINCT PATIENTS WITH T2D AND NO T2D, 7,436,258**/
IF T2D = 0;
RUN;

/**432,513**/
DATA Q2;  /**INNER JOIN**/
MERGE MYHUMANA.T2DXXX (IN = A) /*DISTINCT PATIENTS WITH NO T2D /**7,436,258**/  MYHUMANA.MED(IN = B); /**DISTINCT PATIENTS TAKING ADM**/ /**1,759,104**/
BY PATIENT_ID;
IF A=1 AND B=1;
RUN;

/***CROSS-CHECK WITH ALTERNATE DATA STEP***/

DATA Q2A; /**LEFT JOIN SO ALL PATIENTS TAKING ADM IS IN DATASET Q2A**/ /**1,759,104**/
MERGE MYHUMANA.MED(IN = A) /**1,759,104**/ /*DISTINCT PATIENTS TREATED*/  MYHUMANA.T2DX(IN = B); /**7,436,258**/ /*DISTINCT PATIENTS WITH T2D AND NO T2D**/
BY PATIENT_ID;
IF A=1;
RUN;

/**T2D = 0 = 432,513**/
PROC FREQ DATA = Q2A;
TABLES T2D; 
RUN;



/**Q3**/

/**PATIENTS WITH DIABETES AND RECEIVED AN ANTIDIABETIC MEDICATION**/

/**367,471**/
DATA Q3;
MERGE MYHUMANA.T2DXX (IN = A) /**WITH T2D ONLY**/ /**629,015**/  MYHUMANA.MED(IN = B); /**TAKING ADM ONLY**/ /**1,759,104**/
BY PATIENT_ID;
IF A=1 AND B=1;
RUN;

s there an efficent way i can do this please? THANK YOU

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data disease;
input subjid dx;
cards;
1 1
2 0
3 0
4 1
;
data medication;
input subjid t2D;
cards;
1 0
2 0
3 1
4 1
;

proc sql;
create table p_with_disease_taking_med as
select subjid from disease where dx=1
intersect
select subjid from medication where t2D=1 
;
create table p_with_disease_not_taking_med as
select subjid from disease where dx=1
intersect
select subjid from medication where t2D=0 
;
create table p_without_disease_taking_med as
select subjid from disease where dx=0
intersect
select subjid from medication where t2D=1 
;
quit;

View solution in original post

1 REPLY 1
Ksharp
Super User
data disease;
input subjid dx;
cards;
1 1
2 0
3 0
4 1
;
data medication;
input subjid t2D;
cards;
1 0
2 0
3 1
4 1
;

proc sql;
create table p_with_disease_taking_med as
select subjid from disease where dx=1
intersect
select subjid from medication where t2D=1 
;
create table p_with_disease_not_taking_med as
select subjid from disease where dx=1
intersect
select subjid from medication where t2D=0 
;
create table p_without_disease_taking_med as
select subjid from disease where dx=0
intersect
select subjid from medication where t2D=1 
;
quit;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 344 views
  • 0 likes
  • 2 in conversation