I have a data set which has two time points at 20 and 24 hrs with corresponding Cp values. However in some cases the 24 hour sample may be missing. In those cases I would like to insert a 24 hr time sample with time=24 and cp=0.5*lag(cp) which is the current cp value at 20 hrs. I have written code which does not give an error but it does not work. I need to know how to edit the code to get it to work?
DATA SAMPLE;
INPUT ID TIME CP;
DATALINES;
1 20 27
1 24 15
2 20 50
2 24 10
3 20 30
4 20 15
;
DATA EXPAND (KEEP= ID TIME CP CP1 CP2 );
SET SAMPLE ;
TIME1=LAG(TIME);
TIME2=TIME;
CP1=CP;
CP2=LAG(CP);
OUTPUT;
DO ID=1 TO 4;
IF TIME2 = . THEN DO ;
TIME2=24;
OUTPUT;
CP1=0.5*CP2;
OUTPUT;
END;
END;
PROC PRINT;
RUN;
One way
DATA SAMPLE;
INPUT ID TIME CP;
DATALINES;
1 20 27
1 24 15
2 20 50
2 24 10
3 20 30
4 20 15
;
data want;
set sample;
by id;
output;
if first.id & last.id then do;
TIME=24; CP=CP/2;
output;
end;
run;
Result:
id time CP 1 20 27 1 24 15 2 20 50 2 24 10 3 20 30 3 24 15 4 20 15 4 24 7.5
One way
DATA SAMPLE;
INPUT ID TIME CP;
DATALINES;
1 20 27
1 24 15
2 20 50
2 24 10
3 20 30
4 20 15
;
data want;
set sample;
by id;
output;
if first.id & last.id then do;
TIME=24; CP=CP/2;
output;
end;
run;
Result:
id time CP 1 20 27 1 24 15 2 20 50 2 24 10 3 20 30 3 24 15 4 20 15 4 24 7.5
This?
DATA SAMPLE;
INPUT ID TIME CP;
DATALINES;
1 20 27
1 24 15
2 20 50
2 24 10
3 20 30
4 20 15
;;;;
run;
data want;
set sample;
by id;
output;
if last.id and time ne 24 then do;
time = 24;
cp = .5 *cp;
output;
end;
run;
proc print;
run;
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
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.
Ready to level-up your skills? Choose your own adventure.