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 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;
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

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
data_null__
Jade | Level 19

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;

Capture.PNG

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1142 views
  • 3 likes
  • 3 in conversation