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

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
  • 2 replies
  • 646 views
  • 3 likes
  • 3 in conversation