data have;
infile id date time dose i value;
datalines;
1 8/24/2010 13:07 0 2 1
1 8/24/2010 9:53 0 2 2
1 8/24/2010 13:07 0 2 3
1 8/24/2010 9:53 0 2 4
1 8/24/2010 10:30 1 2 0
1 8/31/2010 9:20 0 2 0
1 8/31/2010 9:20 0 2 0
1 8/31/2010 9:40 1 2 0
;;
data want;
input id date $ time $ dose i value;
datalines;
1 8/24/2010 13:07 0 2 1
1 8/24/2010 9:53 0 2 2
1 8/24/2010 13:07 0 2 3
1 8/24/2010 9:53 0 2 4
1 8/24/2010 10:30 1 2 2
1 8/31/2010 9:20 0 2 2
1 8/31/2010 9:20 0 2 2
1 8/31/2010 9:40 1 2 0
;;
run;
Hi,
the column dose indicates the dosing events (dose=1). I want to assign value=i, if the first dosing event is 1 and value=0 and carry that until the next dose.
Any help?
Thanks
OR you want this ? data have; input id date : $20. time $ dose i value; datalines; 1 8/24/2010 13:07 0 2 1 1 8/24/2010 9:53 0 2 2 1 8/24/2010 13:07 0 2 3 1 8/24/2010 9:53 0 2 4 1 8/24/2010 10:30 1 2 0 1 8/31/2010 9:20 0 2 0 1 8/31/2010 9:20 0 2 0 1 8/31/2010 9:40 1 2 0 ; run; data want; set have; by id; if first.id then n=0; if dose=1 then n+1; if n=1 then value=i; drop n; run;
Do make sure your test data code actually works before posting, there are errors throughout that code. Note in the below I haven't fixed it anymore than necessary to get it working - i.e. dates/times are not right but doesn't affect logic.
data have (drop=value); input id date $ time $ dose i value; datalines; 1 8/24/2010 13:07 0 2 0 1 8/24/2010 9:53 0 2 0 1 8/24/2010 13:07 0 2 0 1 8/24/2010 9:53 0 2 0 1 8/24/2010 10:30 1 2 0 1 8/31/2010 9:20 0 2 0 1 8/31/2010 9:20 0 2 0 1 8/31/2010 9:40 1 2 0 ; run; data want; set have; retain value; by id; if first.id then value=0; if dose=1 and value=0 then value=i; else if dose=1 and value ne 0 then value=0; run;
@RW9: Actually, i do not want to remove the value column before the assignment of values. In the actual dataset, there are already several dosing events and the correponsing "value" per subject. ALso, in some case rows before the first dosing event has corresponsing "value", which should not be changed. I have edited the input datasaet to make it clear. Thanks
Well, you need ot have a variable to retain, so just keep a second one:
data have; input id date $ time $ dose i value; datalines; 1 8/24/2010 13:07 0 2 0 1 8/24/2010 9:53 0 2 0 1 8/24/2010 13:07 0 2 0 1 8/24/2010 9:53 0 2 0 1 8/24/2010 10:30 1 2 0 1 8/31/2010 9:20 0 2 0 1 8/31/2010 9:20 0 2 0 1 8/31/2010 9:40 1 2 0 ; run; data want (drop=v); set have; retain value; by id; if first.id then v=0; if dose=1 and v=0 then v=i; else if dose=1 and v ne 0 then v=0;
if v ne 0 then value=v; run;
@ari wrote:
data have;
infile id date time dose i value;
datalines;
1 8/24/2010 13:07 0 2 1
1 8/24/2010 9:53 0 2 2
1 8/24/2010 13:07 0 2 3
1 8/24/2010 9:53 0 2 4
1 8/24/2010 10:30 1 2 0
1 8/31/2010 9:20 0 2 0
1 8/31/2010 9:20 0 2 0
1 8/31/2010 9:40 1 2 0
;;data want;
input id date $ time $ dose i value;
datalines;
1 8/24/2010 13:07 0 2 1
1 8/24/2010 9:53 0 2 2
1 8/24/2010 13:07 0 2 3
1 8/24/2010 9:53 0 2 4
1 8/24/2010 10:30 1 2 2
1 8/31/2010 9:20 0 2 2
1 8/31/2010 9:20 0 2 2
1 8/31/2010 9:40 1 2 0
;;
run;
I don't see any difference between your have and want data sets
value=2 instead of 0 on rows 5 to 7.
data have; input id date : $20. time $ dose i value; datalines; 1 8/24/2010 13:07 0 2 1 1 8/24/2010 9:53 0 2 2 1 8/24/2010 13:07 0 2 3 1 8/24/2010 9:53 0 2 4 1 8/24/2010 10:30 1 2 0 1 8/31/2010 9:20 0 2 0 1 8/31/2010 9:20 0 2 0 1 8/31/2010 9:40 1 2 0 ; run; data want; set have; by id; retain flag v; if first.id then do;flag=0;v=.;end; if dose=1 then flag=not flag; if dose=1 and flag then v=i; if flag then value=v; drop i v flag; run;
@Ksharp: This works but I want only the first occurance of dose=1 and the subsequent rows until the next dose be changed.
As of now the code changes all the doses and the subsequent rows. Any help? Thanks
You only want change one time. That would be more simple. data have; input id date : $20. time $ dose i value; datalines; 1 8/24/2010 13:07 0 2 1 1 8/24/2010 9:53 0 2 2 1 8/24/2010 13:07 0 2 3 1 8/24/2010 9:53 0 2 4 1 8/24/2010 10:30 1 2 0 1 8/31/2010 9:20 0 2 0 1 8/31/2010 9:20 0 2 0 1 8/31/2010 9:40 1 2 0 ; run; data want; set have; by id; retain v; if first.id then do;n=0;v=.;end; if dose=1 then n+1; if dose=1 and n=1 then v=i; if n=1 then value=v; drop i v n; run;
OR you want this ? data have; input id date : $20. time $ dose i value; datalines; 1 8/24/2010 13:07 0 2 1 1 8/24/2010 9:53 0 2 2 1 8/24/2010 13:07 0 2 3 1 8/24/2010 9:53 0 2 4 1 8/24/2010 10:30 1 2 0 1 8/31/2010 9:20 0 2 0 1 8/31/2010 9:20 0 2 0 1 8/31/2010 9:40 1 2 0 ; run; data want; set have; by id; if first.id then n=0; if dose=1 then n+1; if n=1 then value=i; drop n; run;
@Ksharp: Thanks.
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!
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.