BookmarkSubscribeRSS Feed
ari
Quartz | Level 8 ari
Quartz | Level 8

Hi,

 

idtbasevalue
115.05307814.61624
155.68752936.09745
174.14921945.9342
1143.34242372.15495
    
want   
idtbasevalue
1135.05307867.10187

 

So, Basically I am trying to calculate different between first and last t variable value (14-1=13). similarly, difference (67.10) of last observation of value (72.15) with first value of base (5.05). 

 

Any help to achieve this calculation in sas?

 

Thank you

3 REPLIES 3
Reeza
Super User

Is this by ID or for an entire dataset? 

Assuming ID, use BY group and first/last. 

 

Data want;
Set have;
By id t;
Retain first_value;

If first.id then 
First_value=base;

If last.id then do;
    Diff = base - first_value;
    OUTPUT; * explicitly only output one record per ID - the last one;
end;

Run;
PGStats
Opal | Level 21

Get it with:

 

data have;
input id	t	base	value;
datalines;
1	1	5.053078	14.61624
1	5	5.687529	36.09745
1	7	4.149219	45.9342
1	14	3.342423	72.15495
;

data want;
do until(last.id);
    set have; by id;
    if first.id then do;
        firstT = t;
        startBase = base;
        firstValue = value;
        end;
    t = t - firstT;
    value = value - firstValue;
    end;
drop first: base;
rename startBase=base;
run;

proc print data=want noobs; run;
PG
kenedy_yc
Fluorite | Level 6

/*Create dataset*/
data create_data;
infile datalines;
input id t base value;
datalines;
1 1 5.053078 14.61624
1 5 5.687529 36.09745
1 7 4.149219 45.9342
1 14 3.342423 72.15495
;
run;

data want;
set create_data end=last;
by id t;

/*Initialize variables*/
retain t_first 0
base1 0
t_first 0
t_last 0
value_last 0
t_diff 0
value_diff 0;

/*Read base and t values for first observation*/
if first.id then do;
base1=base;
t_first=t;
end;

/*Read t and value values from last observation*/
if last.id then do;
t_last=t;
value_last=value;
end;

/*Perform calculation*/
t_diff=t_last - t_first;
value_diff=value_last-base1;

/*Subset the dataset for the last observation*/
if last then output;
run;

proc print data=want noobs;
/*Displaying variables of interest*/
var t_first t_last t_diff base1 value_last value_diff;
run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3 replies
  • 6337 views
  • 6 likes
  • 4 in conversation