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

 

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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

10 REPLIES 10
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
ari
Quartz | Level 8 ari
Quartz | Level 8

@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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

 

ballardw
Super User

@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

gamotte
Rhodochrosite | Level 12

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

Ksharp
Super User
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;

ari
Quartz | Level 8 ari
Quartz | Level 8

@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

Ksharp
Super User
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;


Ksharp
Super User
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;


ari
Quartz | Level 8 ari
Quartz | Level 8

@Ksharp: Thanks.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 10 replies
  • 1143 views
  • 4 likes
  • 5 in conversation