BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
jojo
Obsidian | Level 7

I updated the data as below.

 

I need to assign baseline values for different groups based on the time points. For example, assign the value of the last record (value at Day 5) in the S1 group to the baseline value for the next group S2 except for baseline of S1 group which is assigned using the result at Day 1. There are about 30 subjects with 6 records (Day 1 to Day 9). Day 1 and Day 5 are for S1, Day 6 and Day 7 are for S2, and Day 8 and Day 9 for S3.

 

Any input/suggestions will be appreciated.

The current data looks like this:

 


DATA dt;
INPUT ID $ grp $ DAY result ;
CARDS;
001 s1 1 4.1
001 s1 5 5.0
001 s2 6 3.5
001 s2 7 6.2
001 s3 8 2.5
001 s3 9 1.9
002 s1 1 4.3
002 s1 5 5.1
002 s2 6 3.5
002 s2 7 3.9
002 s3 8 2.5
002 s3 9 1.9

;

RUN;

 

new data should include baseline value like this-

use the last obs of the group as the baseline value for the next group. 


DATA newdt;
INPUT ID $ grp $ DAY result baseline ;
CARDS;
001 s1 1 4.1 4.1
001 s1 5 5.0 4.1
001 s2 6 3.5 5.0
001 s2 7 6.2 5.0
001 s3 8 2.5 6.2
001 s3 9 1.9 6.2
002 s1 1 4.3 4.3
002 s1 5 5.1 4.3
002 s2 6 3.5 5.1
002 s2 7 3.9 5.1
002 s3 8 2.5 3.9
002 s3 9 1.9 3.9

;
RUN;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@jojo wrote:
Thanks very much for the reply. I have updated the post with the data/data step code.

 

You description still doesn't mention any exception for use of the changing ID value...

This seems to work for the given example:

data newdt;
   set dt;
   by id grp;
   retain baseline;
   if first.id then baseline=result;
   output;
   if last.grp then baseline=result;
run;

The BY variables create automatic 1/0 (true/false) variables that indicate whether the current observation is the first or last of the By value. These are accessed using the First.<variablename> and Last.<variablename> syntax. The values are not added to the data set unless you assign them to a variable.

Retain tells SAS to keep the values of the variable(s) across the data step boundary.

Your request involves timing of setting the value vs when the data is written to the output data. So write the data and then reset the new baseline when needed.

The First.ID sets the baseline when a new Id is encountered with the first result value.

 

 

View solution in original post

5 REPLIES 5
ballardw
Super User

Details please. You say "use day 5 value as baseline value for group 1 and day 8 value as baseline value for group2". What in the data set tells us that those are the days to use? Otherwise all we can do is provide a baseline for group 1 and 2. If you have groups 3, 4, 5 or more there is nothing to tell us which value(s) to use.

 

Also you should provide your example data in the form of data step code so we don't have  to guess as to variable types.

 


@jojo wrote:

need to assign baseline values for different groups based on the time points. Any input/suggestions will be appreciated.

The current data looks like this:

 

id         day       value   group

1           1             4             .

1          5             3             .

1          6             6            1

1          7             5            1

1          8             3            1

1          9              1            2

1          10            2           2

2          1             3            .

2          5             4             .

2         6             3            1

2         7             6            1

2          8             3            1

2         9              3            2

2          10            6           2

 

new data should look like this: use day 5 value as baseline value for group 1 and day 8 value as baseline value for group2

id         day       value   group      baseline

1           1             4             .                .

1          5             3             .                 3

1          6             6            1                 3

1          7             5            1                 3

1          8             4           1                 3

1          9              1            2                4

1          10            2           2                 4

2          1             3            .                  .

2          5             4             .               .

2         6             3            1                4

2         7             6            1                4

2          8             3            1               4

2         9              3            2               3

2          10            6           2               3


 

jojo
Obsidian | Level 7
Thanks very much for the reply. I have updated the post with the data/data step code.
ballardw
Super User

@jojo wrote:
Thanks very much for the reply. I have updated the post with the data/data step code.

 

You description still doesn't mention any exception for use of the changing ID value...

This seems to work for the given example:

data newdt;
   set dt;
   by id grp;
   retain baseline;
   if first.id then baseline=result;
   output;
   if last.grp then baseline=result;
run;

The BY variables create automatic 1/0 (true/false) variables that indicate whether the current observation is the first or last of the By value. These are accessed using the First.<variablename> and Last.<variablename> syntax. The values are not added to the data set unless you assign them to a variable.

Retain tells SAS to keep the values of the variable(s) across the data step boundary.

Your request involves timing of setting the value vs when the data is written to the output data. So write the data and then reset the new baseline when needed.

The First.ID sets the baseline when a new Id is encountered with the first result value.

 

 

jojo
Obsidian | Level 7

Thanks very much. It works for my data. 

jojo
Obsidian | Level 7

I updated the data below. The order of grp is not the same as the order of DAY, and I need to assign the baseline value based on the order of day. I tried to add DAY in by statement, but it doesn't work. 

 

DATA dt;
INPUT ID $ grp $ DAY result ;
CARDS;
001 s2 1 4.1
001 s2 5 5.0
001 s1 6 3.5
001 s1 7 6.2
001 s3 8 2.5
001 s3 9 1.9
002 s2 1 4.3
002 s2 5 5.1
002 s1 6 3.5
002 s1 7 3.9
002 s3 8 2.5
002 s3 9 1.9

;

RUN;

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1085 views
  • 0 likes
  • 2 in conversation