BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jacksonan123
Lapis Lazuli | Level 10

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?

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Run the test that I provided and you will see that table test gets updated with the desired values.

PG

View solution in original post

8 REPLIES 8
jacksonan123
Lapis Lazuli | Level 10
Ok, I will write some code using the lag function to resolve the issue and
if successful, I will post the results.
PGStats
Opal | Level 21

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)?

PG
jacksonan123
Lapis Lazuli | Level 10
For each subject there will be only one value at 20 hr. and at 24 hr.
jacksonan123
Lapis Lazuli | Level 10
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

410.
45.
4105.0
4155.0
4105.0
4207.5
PGStats
Opal | Level 21

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;
PG
jacksonan123
Lapis Lazuli | Level 10

The resulting Table20 did not have values for 24 hr.  Where can I find that table?

Obs Subject Time DV123

12010
22010
32020
PGStats
Opal | Level 21

Run the test that I provided and you will see that table test gets updated with the desired values.

PG

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 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
  • 8 replies
  • 661 views
  • 0 likes
  • 3 in conversation