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 out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.