Dear
I have a dataset with the following variables: patient ID and dose.
I would like to create a new variable called increase based on the Dose variable.
This variable should be set to 1 when the dose in the next row (for the same patient) is higher.
Example
pt_ID DOSE increase
1 3 1
1 4 0
1 3 0
2 24 0
2 22 1
2 25 0
Thank in advance for any help
data have;
input pt_ID DOSE;
datalines;
1 3
1 4
1 3
2 24
2 22
2 25
;
data want;
merge have have(firstobs=2 keep=dose pt_id rename=(dose=next_dose pt_id=next_pt_id));
increase=0;
if pt_id=next_pt_id and next_dose>dose then increase=1;
drop next_:;
run;
Please from now on, provide data as data step code, as I show above. Please do not provide data in other forms.
data have;
input pt_ID DOSE;
datalines;
1 3
1 4
1 3
2 24
2 22
2 25
;
data want;
merge have have(firstobs=2 keep=dose pt_id rename=(dose=next_dose pt_id=next_pt_id));
increase=0;
if pt_id=next_pt_id and next_dose>dose then increase=1;
drop next_:;
run;
Please from now on, provide data as data step code, as I show above. Please do not provide data in other forms.
Hi,
I had essentially the same solution as @PaigeMiller, with a slight difference in the method used to assign the increase variable.
data have;
input
pt_id
dose
;
datalines;
1 3
1 4
1 3
2 24
2 22
2 25
;
data want(drop = next:);
merge
have
have(firstobs=2 rename = (pt_id = next_pt_id dose = next_dose))
;
increase = next_pt_id eq pt_id and next_dose gt dose;
run;
Thanks & kind regards,
Amir.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.