BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tSAS1
Obsidian | Level 7

I have a dataset like this: 

Patient IDDrugdatedays of consumptiondose
1A01/01/2018281056
1A29/01/201821800
1A19/02/2018282000
1B05/03/20182812000
1A02/04/201821900
2A03/03/2019281300

I want to transform the data into weeks of consumption, based on the period of drugA and the dataset that i want is like shown below:

Patient IDDrugWeekweekly_dose
1A1264
1A2264
1A3264
1A4264
1A5266.6666667
1A6266.6666667
1A7266.6666667
1A8500
1A9500
1A10500
1A11500
1B103000
1B113000
1A120
1A130
1B123000
1B133000
1A14300
1A15300
1A16300
2A1325
2A2325
2A3325
2A4325

 

My code is not really helpful because I created the data per weeks, and not based on the weeks of drug A

data DrugA;
set data(where=(drug="A"));
do i=1 to (days_cosumption / 7);
		weeks=cats("Week",i);
		weekly_dose=dose/(days_cosumption / 7);
		output;
	end;
run;
data DrugB;
set data(where=(drug="B"));
do i=1 to (days_cosumption / 7);
		weeks=cats("Week",i);
		weekly_dose=dose/(days_cosumption / 7);
		output;
	end;
run;
data Drug_A_B;
set DrugA DrugB;
run;

 

and this is to plot the consumption of the two medications for each ID over drugA weeks as this example of plot

 

tSAS1_0-1614852416152.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

With data you provided, this seems to be doing the job:

data have;
input patient_ID	Drug $	date ddmmyy10.	days_of_consumption	dose;
format date ddmmyy10.;
cards;
1	A	01/01/2018	28	1056
1	A	29/01/2018	21	800
1	A	19/02/2018	28	2000
1	B	05/03/2018	28	12000
1	A	02/04/2018	21	900
2	A	03/03/2019	28	1300
;
run;
proc print;
run;

data want1;
  set have;

  dt = date;
  d = drug;
  do _N_ = 0 to days_of_consumption-1;
    daily_dose = dose/days_of_consumption;
    date = dt + _N_; 
    drug = d;
    output;
    if d ^= "A" then     /* hardcoding */
      do;
        drug = "A";      /* hardcoding */
        daily_dose = 0;  /* hardcoding */
        output;
      end;
  end;
  drop weeks days_of_consumption dt dose d;
run;

proc sort data = want1;
  by patient_ID date drug;
run;

data want2;
  set want1;
  by patient_ID date;
  if first.patient_ID then week = 0;
  days + first.date;
  week + (mod(days,7)=1)*first.date;
run;

proc sql;
create table want3 as
  select patient_ID, drug, week, sum(daily_dose) as weekly_dose
  from want2
  group by 1,2,3
  order by 1,3,2
; 
quit;

proc print;
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

4 REPLIES 4
andreas_lds
Jade | Level 19

You may want to explain, why week starts with 10 (10 seems to be the number of the week) for

1 B 05/03/2018 28 12000

but with 1 for

2 A 03/03/2019 28 1300

 

tSAS1
Obsidian | Level 7

@andreas_lds  for the first ID this line
1 A 19/02/2018 28 2000
means that starting from week8 the patient1 started consuming DrugA for 4weeks (from week8 to week11) until 19/03/2018 and he started consuming DrugB starting from 05/03/2018 for 4weeks.
it means he consumed both drugs in same two weeks (week10 and 11),

In other words he started taking drugB from the 10th week of drugA.

 

And patient2 took only drugA for 4weeks, that's why his weeks started from 1 until 4.

yabwon
Onyx | Level 15

With data you provided, this seems to be doing the job:

data have;
input patient_ID	Drug $	date ddmmyy10.	days_of_consumption	dose;
format date ddmmyy10.;
cards;
1	A	01/01/2018	28	1056
1	A	29/01/2018	21	800
1	A	19/02/2018	28	2000
1	B	05/03/2018	28	12000
1	A	02/04/2018	21	900
2	A	03/03/2019	28	1300
;
run;
proc print;
run;

data want1;
  set have;

  dt = date;
  d = drug;
  do _N_ = 0 to days_of_consumption-1;
    daily_dose = dose/days_of_consumption;
    date = dt + _N_; 
    drug = d;
    output;
    if d ^= "A" then     /* hardcoding */
      do;
        drug = "A";      /* hardcoding */
        daily_dose = 0;  /* hardcoding */
        output;
      end;
  end;
  drop weeks days_of_consumption dt dose d;
run;

proc sort data = want1;
  by patient_ID date drug;
run;

data want2;
  set want1;
  by patient_ID date;
  if first.patient_ID then week = 0;
  days + first.date;
  week + (mod(days,7)=1)*first.date;
run;

proc sql;
create table want3 as
  select patient_ID, drug, week, sum(daily_dose) as weekly_dose
  from want2
  group by 1,2,3
  order by 1,3,2
; 
quit;

proc print;
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 759 views
  • 0 likes
  • 3 in conversation