- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I have the following data.
Subject | Time | DV |
1 | 20 | 10 |
1 | 24 | 5 |
2 | 20 | 10 |
2 | 24 | 15 |
2 | 20 | 20 |
2 | 24 | 30 |
I would like to do the following:
1.compare DV values at 20 and 24 hrs
2.whenever the DV value at 24 hr is greater than the DV at 20 hr delete the value at 24 hrs. and substitute the value (0.5x DV at 20 hr).
How do I accomplish this task?
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Run the test that I provided and you will see that table test gets updated with the desired values.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Use the lag() function to retrieve the value of a variable in the previous observation.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
if successful, I will post the results.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
What happens when there are multiple values at time=20 for a given subject? Which one should be used in the calculation of (0.5x DV at 20 hr)?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data new;
input subject time dv;
datalines;
1 20 10
1 24 5
2 20 10
2 24 15
3 20 10
3 24 20
;
run;
data old;
set new;
retain subject dv lagdv x;
do subject=1 to 3;
if time in (20 24);
lagdv=lag(dv);
if dv>lag(dv) then x=0.5*lag(dv);
end;
output;
keep subject dv x;
run;
I ran this code and the result had an issue with outputting the correct subject's number. Can someone correct the code for me so that subjects 1-3 will be output?
Obs subject dv x123456
4 | 10 | . |
4 | 5 | . |
4 | 10 | 5.0 |
4 | 15 | 5.0 |
4 | 10 | 5.0 |
4 | 20 | 7.5 |
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
SQL will be more robust to unforeseen cases than a data step. Try this:
data test;
input Subject Time DV;
datalines;
1 20 10
1 24 5
2 20 10
2 24 15
3 20 20
3 24 30
;
proc sql;
create table test20 as
select * from test where time=20;
update test as a
set DV = (select mean(0.5*DV) from test20 where subject=a.subject)
where time = 24 and DV > (select mean(DV) from test20 where subject=a.subject);
drop table test20;
quit;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The resulting Table20 did not have values for 24 hr. Where can I find that table?
Obs Subject Time DV123
1 | 20 | 10 |
2 | 20 | 10 |
3 | 20 | 20 |
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Run the test that I provided and you will see that table test gets updated with the desired values.