BookmarkSubscribeRSS Feed
SAS_Learner2
Calcite | Level 5

Hello Friends,

I am facing challenge to get flagging variables to be derived.

 

Have data:
ID Body_Part Rash_Date
001 SR 18APR2022:20:00:00
001 SR 24OCT2022:19:01:00
001 SR 04NOV2021:21:00:00
001 SR 05AUG2022:18:00:00
001 SR,KL 05OCT2021:21:00:00
001 SR,KL 09OCT2021:14:14:00
001 EL 29JAN2022:21:00:00
001 EL 04NOV2021:21:00:00
001 KL 14DEC2022:19:00:00
001 KL 26MAR2022:12:04:00
001 KL 05APR2022:21:00:00
001 KL 14FEB2022:21:00:00
001 KL 13SEP2022:19:26:00
001 KL 28NOV2021:18:42:00
001 KL 04FEB2022:09:00:00
001 KL 05AUG2022:18:00:00
001 KL 15SEP2021:15:00:00
001 KL 30JAN2022:23:27:00
001 KL 24SEP2021:21:04:00
001 KL 20SEP2022:08:58:00
001 KL 30NOV2021:20:59:00
001 KL 24OCT2022:19:01:00
001 KL 01DEC2021:20:53:00
001 KL 29MAR2022:22:38:00
001 KL 29DEC2021:00:38:00
001 KL 13APR2022:18:35:00
001 KL 28JUN2022:19:00:00
001 KL 09NOV2022:19:59:00
001 KL 22NOV2021:23:37:00
001 KL 22JUN2022:19:19:00
001 KL 09JUN2022:17:02:00
001 KL 17JAN2022:21:00:00
001 KL 12JUN2022:22:01:00
001 KL 15FEB2022:13:51:00
001 KL 21JAN2022:21:00:00
001 KL 01NOV2021:21:00:00
001 KL 24JAN2022:15:57:00
001 KL 15MAY2022:08:01:00
001 KL 10JAN2022:19:40:00
001 KL 10MAY2022:19:01:00
001 KL 10AUG2022:15:00:00
001 KL 03MAY2022:18:00:00
001 KL 19OCT2022:19:54:00
001 KL 13FEB2022:21:00:00
001 KL 07JAN2022:21:00:00
001 KL 02MAY2022:18:17:00
001 KL 22SEP2021:21:57:00
001 KL 30SEP2021:21:00:00
001 KL 05JAN2022:00:13:00
001 CR 19OCT2022:20:54:00
001 CR 13SEP2022:19:26:00
001 CR 28JUN2022:19:00:00
001 CR, SR 22JUN2022:19:19:00
001 SR 05AUG2022:18:01:00
001 SR,FR,S U_R 28NOV2021:16:42:00
001 TR 02MAY2022:17:18:00

1. I want to create a flag whenever there is a rash on new body Part.

data want1 (drop = Body_Part);

set have;
by id Body_Part Rash_Date;
do i = 1 to while (scan(body_Part,i,",") ^= ' ');
n_body_Part = scan(body_Part,i, ' ');
output;
end;
run;

data want2;
set want1(rename = body_Part=n_body_Part);
by id body_Part Rash_date;
if first.bodyPart then A1 = "Y";
run;

Output will come like below:

 

ID Body_Part Rash_Date New_Body_Part
001 SR 18APR2022:20:00:00 Y
001 SR 22JUN2022:19:19:00
001 SR 05AUG2022:18:00:00
001 SR 24OCT2022:19:01:00
001 KL 26MAR2022:12:04:00 Y
001 KL 29MAR2022:22:38:00
001 KL 05APR2022:21:00:00
001 KL 13APR2022:18:35:00
001 KL 02MAY2022:18:17:00
001 KL 02MAY2022:18:17:00
001 KL 03MAY2022:18:00:00
001 KL 10MAY2022:19:01:00
001 KL 15MAY2022:08:01:00
001 KL 09JUN2022:17:02:00
001 KL 12JUN2022:22:01:00
001 KL 22JUN2022:19:19:00
001 KL 28JUN2022:19:00:00
001 KL 05AUG2022:18:00:00
001 KL 10AUG2022:15:00:00
001 KL 13SEP2022:19:26:00
001 KL 20SEP2022:08:58:00
001 KL 19OCT2022:19:54:00
001 KL 24OCT2022:19:01:00
001 KL 09NOV2022:19:59:00
001 KL 14DEC2022:19:00:00
001 KR 22JUN2022:19:19:00 Y
001 KR 28JUN2022:19:00:00
001 KR 13SEP2022:19:26:00
001 KR 19OCT2022:20:54:00
001 SR 05AUG2022:18:01:00 Y
001 TR 02MAY2022:17:18:00 Y
001 TR 02MAY2022:17:18:00
001 SR 05OCT2021:21:00:00 Y
001 SR 05OCT2021:21:00:00
001 SR 09OCT2021:14:14:00
001 SR 04NOV2021:21:00:00
001 A UR 28NOV2021:16:42:00 Y
001 A UR 28NOV2021:16:42:00
001 EL 04NOV2021:21:00:00 Y
001 EL 29JAN2022:21:00:00
001 EL 29JAN2022:21:00:00
001 FR 28NOV2021:16:42:00 Y
001 FR 28NOV2021:16:42:00
001 KL 15SEP2021:15:00:00 Y
001 KL 22SEP2021:21:57:00
001 KL 22SEP2021:21:57:00
001 KL 24SEP2021:21:04:00
001 KL 24SEP2021:21:04:00
001 KL 30SEP2021:21:00:00
001 KL 30SEP2021:21:00:00
001 KL 05OCT2021:21:00:00
001 KL 05OCT2021:21:00:00
001 KL 09OCT2021:14:14:00
001 KL 01NOV2021:21:00:00
001 KL 22NOV2021:23:37:00
001 KL 22NOV2021:23:37:00
001 KL 28NOV2021:18:42:00
001 KL 28NOV2021:18:42:00
001 KL 30NOV2021:20:59:00
001 KL 30NOV2021:20:59:00
001 KL 01DEC2021:20:53:00
001 KL 29DEC2021:00:38:00
001 KL 05JAN2022:00:13:00
001 KL 07JAN2022:21:00:00
001 KL 10JAN2022:19:40:00
001 KL 17JAN2022:21:00:00
001 KL 21JAN2022:21:00:00
001 KL 24JAN2022:15:57:00
001 KL 30JAN2022:23:27:00
001 KL 04FEB2022:09:00:00
001 KL 13FEB2022:21:00:00
001 KL 13FEB2022:21:00:00
001 KL 14FEB2022:21:00:00
001 KL 14FEB2022:21:00:00
001 KL 15FEB2022:13:51:00
001 SR 28NOV2021:16:42:00 Y
001 SR 28NOV2021:16:42:00

 

2. I want to create another flag variable Repeat_Part when the hours difference between Rash date time and last drug adminstration date time for the same body Part if it is more than 50 hours then Repeat Part = "Y"

There can be multiple rashes at differnt body Parts.

ID Body_Part Rash_Date New_Body_Part cur_rashdt_lst_inf_date Repeat_Part
001 SR 18APR2022:20:00:00 Y
001 SR 22JUN2022:19:19:00
001 SR 05AUG2022:18:00:00
001 SR 24OCT2022:19:01:00
001 KL 26MAR2022:12:04:00 Y
001 KL 29MAR2022:22:38:00
001 KL 05APR2022:21:00:00
001 KL 13APR2022:18:35:00
001 KL 02MAY2022:18:17:00
001 KL 02MAY2022:18:17:00
001 KL 03MAY2022:18:00:00
001 KL 10MAY2022:19:01:00
001 KL 15MAY2022:08:01:00
001 KL 09JUN2022:17:02:00
001 KL 12JUN2022:22:01:00
001 KL 22JUN2022:19:19:00
001 KL 28JUN2022:19:00:00
001 KL 05AUG2022:18:00:00
001 KL 10AUG2022:15:00:00
001 KL 13SEP2022:19:26:00
001 KL 20SEP2022:08:58:00
001 KL 19OCT2022:19:54:00
001 KL 24OCT2022:19:01:00
001 KL 09NOV2022:19:59:00
001 KL 14DEC2022:19:00:00
001 KR 22JUN2022:19:19:00 Y
001 KR 28JUN2022:19:00:00
001 KR 13SEP2022:19:26:00
001 KR 19OCT2022:20:54:00
001 SR 05AUG2022:18:01:00 Y
001 TR 02MAY2022:17:18:00 Y
001 TR 02MAY2022:17:18:00
001 SR 05OCT2021:21:00:00 Y
001 SR 05OCT2021:21:00:00
001 SR 09OCT2021:14:14:00
001 SR 04NOV2021:21:00:00
001 A UR 28NOV2021:16:42:00 Y
001 A UR 28NOV2021:16:42:00
001 EL 04NOV2021:21:00:00 Y
001 EL 29JAN2022:21:00:00
001 EL 29JAN2022:21:00:00
001 FR 28NOV2021:16:42:00 Y
001 FR 28NOV2021:16:42:00
001 KL 15SEP2021:15:00:00 Y
001 KL 22SEP2021:21:57:00
001 KL 22SEP2021:21:57:00
001 KL 24SEP2021:21:04:00
001 KL 24SEP2021:21:04:00
001 KL 30SEP2021:21:00:00
001 KL 30SEP2021:21:00:00
001 KL 05OCT2021:21:00:00
001 KL 05OCT2021:21:00:00
001 KL 09OCT2021:14:14:00
001 KL 01NOV2021:21:00:00
001 KL 22NOV2021:23:37:00
001 KL 22NOV2021:23:37:00
001 KL 28NOV2021:18:42:00
001 KL 28NOV2021:18:42:00
001 KL 30NOV2021:20:59:00
001 KL 30NOV2021:20:59:00
001 KL 01DEC2021:20:53:00
001 KL 29DEC2021:00:38:00
001 KL 05JAN2022:00:13:00
001 KL 07JAN2022:21:00:00
001 KL 10JAN2022:19:40:00
001 KL 17JAN2022:21:00:00
001 KL 21JAN2022:21:00:00
001 KL 24JAN2022:15:57:00
001 KL 30JAN2022:23:27:00
001 KL 04FEB2022:09:00:00
001 KL 13FEB2022:21:00:00
001 KL 13FEB2022:21:00:00
001 KL 14FEB2022:21:00:00
001 KL 14FEB2022:21:00:00
001 KL 15FEB2022:13:51:00
001 SR 28NOV2021:16:42:00 Y
001 SR 28NOV2021:16:42:00

3. Once repeat_Part creted with Y when condition satisfies.. I want to further check the rashes which is coming at same body Part and its rash datetime difference not falling < 50 hours when compSRed with New_Body_Part="Y" or Repeat_Rash = Y its Drug adminstration date. If duration between repeated rash at same locaiton date time minus last rash (which has New_Body_Part = "Y" or Repeat_Part = "Y") hours differnce is more than 50 hours I want flag "Y" for RepeaTRash_gt50hours.

ID BodyPart Rash_Date New_Body_Part cur_rashdt_lst_inf_date Repeat_Part RepeaTRash_gt50hours
001 SR 18APR2022:20:00:00 Y
001 SR 22JUN2022:19:19:00
001 SR 05AUG2022:18:00:00
001 SR 24OCT2022:19:01:00
001 KL 26MAR2022:12:04:00 Y
001 KL 29MAR2022:22:38:00
001 KL 05APR2022:21:00:00
001 KL 13APR2022:18:35:00
001 KL 02MAY2022:18:17:00
001 KL 02MAY2022:18:17:00
001 KL 03MAY2022:18:00:00
001 KL 10MAY2022:19:01:00
001 KL 15MAY2022:08:01:00
001 KL 09JUN2022:17:02:00
001 KL 12JUN2022:22:01:00
001 KL 22JUN2022:19:19:00
001 KL 28JUN2022:19:00:00
001 KL 05AUG2022:18:00:00
001 KL 10AUG2022:15:00:00
001 KL 13SEP2022:19:26:00
001 KL 20SEP2022:08:58:00
001 KL 19OCT2022:19:54:00
001 KL 24OCT2022:19:01:00
001 KL 09NOV2022:19:59:00
001 KL 14DEC2022:19:00:00
001 KR 22JUN2022:19:19:00 Y
001 KR 28JUN2022:19:00:00
001 KR 13SEP2022:19:26:00
001 KR 19OCT2022:20:54:00
001 SR 05AUG2022:18:01:00 Y
001 TR 02MAY2022:17:18:00 Y
001 TR 02MAY2022:17:18:00
001 SR 05OCT2021:21:00:00 Y
001 SR 05OCT2021:21:00:00
001 SR 09OCT2021:14:14:00
001 SR 04NOV2021:21:00:00
001 A UR 28NOV2021:16:42:00 Y
001 A UR 28NOV2021:16:42:00
001 EL 04NOV2021:21:00:00 Y
001 EL 29JAN2022:21:00:00
001 EL 29JAN2022:21:00:00
001 FR 28NOV2021:16:42:00 Y
001 FR 28NOV2021:16:42:00
001 KL 15SEP2021:15:00:00 Y
001 KL 22SEP2021:21:57:00
001 KL 22SEP2021:21:57:00
001 KL 24SEP2021:21:04:00
001 KL 24SEP2021:21:04:00
001 KL 30SEP2021:21:00:00
001 KL 30SEP2021:21:00:00
001 KL 05OCT2021:21:00:00
001 KL 05OCT2021:21:00:00
001 KL 09OCT2021:14:14:00
001 KL 01NOV2021:21:00:00
001 KL 22NOV2021:23:37:00
001 KL 22NOV2021:23:37:00
001 KL 28NOV2021:18:42:00
001 KL 28NOV2021:18:42:00
001 KL 30NOV2021:20:59:00
001 KL 30NOV2021:20:59:00
001 KL 01DEC2021:20:53:00
001 KL 29DEC2021:00:38:00
001 KL 05JAN2022:00:13:00
001 KL 07JAN2022:21:00:00
001 KL 10JAN2022:19:40:00
001 KL 17JAN2022:21:00:00
001 KL 21JAN2022:21:00:00
001 KL 24JAN2022:15:57:00
001 KL 30JAN2022:23:27:00
001 KL 04FEB2022:09:00:00
001 KL 13FEB2022:21:00:00
001 KL 13FEB2022:21:00:00
001 KL 14FEB2022:21:00:00
001 KL 14FEB2022:21:00:00
001 KL 15FEB2022:13:51:00
001 SR 28NOV2021:16:42:00 Y
001 SR 28NOV2021:16:42:00

Notes to consider while creting Repeat_Part variable:
a.
If single body Part rash have multiple drug admistrations then take max of from the list drug adminstation (03MAY2022:21:00:00) into consideraiton to calculate hours difference.
vis.
KL 02MAY2022:18:17:00 03MAY2022:00:17:00
KL 02MAY2022:18:17:00 03MAY2022:21:00:00

It has multiple adminstraitons for same rash. so we hav to consider 03MAY2022:21:00:00 only this record in final dataset and calculaitons.


b. If multiple body Parts has same datetime rashes then we have to consider minimum of any one of the rash last drung adminstation datetime to calculate hours differnce with previous rash.

001 SR,FR,A UR 28NOV2021:16:42:00

This record has multple body Parts rash at same datetime.
First we have look for SR,FR,A UR drug adminstration datetime.
a. then take maximum of each body Part if either of the Part has multile adminstration for same rash (as explained in point 4)
b. then we will have one record for each rash with max drug adminstration datetime.
c. from this data take min drug adminstation of these 3 body Parts.
d. and calculate hour differnce between current rash date - previous last drug adminstration datetime (c point date).

4. My final dataset should have
8 variables:
1.ID
2.Body_Part
3.Rash_Date
4.New_Body_Part
5.Cur_rashdt_Minus_lst_inf_date_hours
6.Repeat_Part
7.currenTRash_date_minus_lasTRepeaTRash_eq_Y_durg adminstration date in hour.
8. RepeaTRash_gt_50hours.

If rashs happended at multple body Parts at same then it should come only in one record.

ID Body_Part Rash_Date
001 KL 15SEP2021:15:00:00
001 KL 22SEP2021:21:57:00
001 KL 24SEP2021:21:04:00
001 KL 30SEP2021:21:00:00
001 SR,KL 05OCT2021:21:00:00
001 SR,KL 09OCT2021:14:14:00
001 KL 01NOV2021:21:00:00
001 EL 04NOV2021:21:00:00
001 SR 04NOV2021:21:00:00
001 KL 22NOV2021:23:37:00
001 SR,FR,A UR 28NOV2021:16:42:00
001 KL 28NOV2021:18:42:00
001 KL 30NOV2021:20:59:00
001 KL 01DEC2021:20:53:00
001 KL 29DEC2021:00:38:00
001 KL 05JAN2022:00:13:00
001 KL 07JAN2022:21:00:00
001 KL 10JAN2022:19:40:00
001 KL 17JAN2022:21:00:00
001 KL 21JAN2022:21:00:00
001 KL 24JAN2022:15:57:00
001 EL 29JAN2022:21:00:00
001 KL 30JAN2022:23:27:00
001 KL 04FEB2022:09:00:00
001 KL 13FEB2022:21:00:00
001 KL 14FEB2022:21:00:00
001 KL 15FEB2022:13:51:00
001 KL 26MAR2022:12:04:00
001 KL 29MAR2022:22:38:00
001 KL 05APR2022:21:00:00
001 KL 13APR2022:18:35:00
001 SR 18APR2022:20:00:00
001 TR 02MAY2022:17:18:00
001 KL 02MAY2022:18:17:00
001 KL 03MAY2022:18:00:00
001 KL 10MAY2022:19:01:00
001 KL 15MAY2022:08:01:00
001 KL 09JUN2022:17:02:00
001 KL 12JUN2022:22:01:00
001 KL 22JUN2022:19:19:00
001 CR, SR 22JUN2022:19:19:00
001 KL 28JUN2022:19:00:00
001 CR 28JUN2022:19:00:00
001 KL 05AUG2022:18:00:00
001 SR 05AUG2022:18:00:00
001 SR 05AUG2022:18:01:00
001 KL 10AUG2022:15:00:00
001 CR 13SEP2022:19:26:00
001 KL 13SEP2022:19:26:00
001 KL 20SEP2022:08:58:00
001 KL 19OCT2022:19:54:00
001 CR 19OCT2022:20:54:00
001 SR 24OCT2022:19:01:00
001 KL 24OCT2022:19:01:00
001 KL 09NOV2022:19:59:00
001 KL 14DEC2022:19:00:00

 

1 REPLY 1
Seadrago
Obsidian | Level 7

section 1) Creating a flag for each new instance of a rash on a different body part

Instead of doing a loop I would approach this with a first.

 

proc sort data=have;

  by ID Body_Part Rash_Date;

run;

 

data want;

  set have;

  by ID Body_Part Rash_Date;

  if first.Body_Part then New_Body_Part="Y";

run;

 

Section 2)  creating flag variable Repeat_Part when the hours difference between Rash date time and last drug adminstration date time for the same body Part if it is more than 50 hours then Repeat Part = "Y". There can be multiple rashes at different body Parts.

 

For this section you would need the variable for last drug administration date-time and merge it with original dataset.

Then you can derive the number of hours between rash date-time and last drug administration date-time.

I would approach this with a macro for each body part.

 

%macro repeat_fl(body_part=);

data tmp;

  set have;

  if body_part="&body_part.";

  if n(rash_date, drug_admin_date)=2 then hr_diff=intck('hour', rash_date, durg_admin_date);

  if hr_diff>50 then repeat_part="Y";

run;

%mend repeat_fl;

 

  

 

 

 

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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