## Assign value based on first dose

Solved
Frequent Contributor
Posts: 106

# 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: 10,778

## 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;

```

All Replies
Super User
Posts: 9,599

## 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
Posts: 106

## 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
Posts: 9,599

## 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: 13,542

## 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

Super Contributor
Posts: 340

## Re: Assign value based on first dose

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

Super User
Posts: 10,778

## 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
Posts: 106

## 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: 10,778

## 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: 10,778

## 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
Posts: 106

## Re: Assign value based on first dose

@Ksharp: Thanks.

☑ This topic is solved.