DATA Step, Macro, Functions and more

Assign value based on first dose

Accepted Solution Solved
Reply
Frequent Contributor
Frequent Contributor
Posts: 101
Accepted Solution

Assign value based on first dose

[ Edited ]

 

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


Accepted Solutions
Solution
‎03-16-2017 02:32 PM
Super User
Posts: 9,671

Re: Assign value based on first dose

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;


View solution in original post


All Replies
Super User
Super User
Posts: 7,392

Re: Assign value based on first dose

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;
Frequent Contributor
Frequent Contributor
Posts: 101

Re: Assign value based on first dose

@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

Super User
Super User
Posts: 7,392

Re: Assign value based on first dose

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;

 

Super User
Posts: 10,466

Re: Assign value based on first dose


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

Regular Contributor
Posts: 194

Re: Assign value based on first dose

value=2 instead of 0 on rows 5 to 7.

Super User
Posts: 9,671

Re: Assign value based on first dose

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;

Frequent Contributor
Frequent Contributor
Posts: 101

Re: Assign value based on first dose

@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

Super User
Posts: 9,671

Re: Assign value based on first dose

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;


Solution
‎03-16-2017 02:32 PM
Super User
Posts: 9,671

Re: Assign value based on first dose

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;


Frequent Contributor
Frequent Contributor
Posts: 101

Re: Assign value based on first dose

@Ksharp: Thanks.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 152 views
  • 4 likes
  • 5 in conversation