Hi All,
I need to subtract 200 variables(heat, speed, weight......) for time points(7-0, 15-0) for 40 subjects(jack, ben, carol....). What would be the best way to do that?
My data set looks like this
subjects | time | heat | speed | weight |
jack | 0 | 60 | 20 | 179 |
jack | 7 | 50 | 25 | 179 |
jack | 15 | 60 | 23 | 175 |
ben | 0 | 48 | 30 | 160 |
ben | 7 | 62 | 32 | 159 |
ben | 15 | 57 | 29 | 156 |
carol | 0 | 39 | 30 | 136 |
carol | 7 | 50 | 27 | 136 |
carol | 15 | 63 | 24 | 134 |
I want table like this
subjects | time | heat | speed | weight |
jack | 0 | 60 | 20 | 179 |
jack | 7 | 50 | 25 | 179 |
jack | 15 | 60 | 23 | 175 |
ben | 0 | 48 | 30 | 160 |
ben | 7 | 62 | 32 | 159 |
ben | 15 | 57 | 29 | 156 |
carol | 0 | 39 | 30 | 136 |
carol | 7 | 50 | 27 | 136 |
carol | 15 | 63 | 24 | 134 |
jack | _7/0 | -10 | -5 | 0 |
jack | _15/0 | 0 | -3 | -4 |
ben | _7/0 | 14 | 2 | -1 |
ben | _15/0 | 9 | -1 | -4 |
Here is a simple way, using proc transpose twice:
proc transpose data=have out=t1;
by subject notsorted;
id time;
var heat -- weight;
run;
data t2;
set t1;
_7_0 = _7 - _0;
_15_0 = _15 - _0;
run;
proc transpose data=t2 out=want name=time;
by subject notsorted;
id _name_;
var _7_0 _15_0;
run;
PG
This line:
jack | _7/0 | -10 | -5 | 0 |
has 2nd-first for heat and weigh but first-2nd for speed. Is that correct? You say you have 200 variables are they all order dependent or did you actually mean to subtract the first from the second in all cases?
Also is time currently numeric? If so it will have to change to character to accommodate values like _7/0.
Yes, that is correct!
Yes, subtract first from the second and third. so subtract time 0 from time 7 and time 15 for all .
No time is character.
Thanks!
Here is a simple way, using proc transpose twice:
proc transpose data=have out=t1;
by subject notsorted;
id time;
var heat -- weight;
run;
data t2;
set t1;
_7_0 = _7 - _0;
_15_0 = _15 - _0;
run;
proc transpose data=t2 out=want name=time;
by subject notsorted;
id _name_;
var _7_0 _15_0;
run;
PG
Thank You!
If your table was big .
data have; input subject $ time heat speed weight ; cards; jack 0 60 20 179 jack 7 50 25 179 jack 15 60 23 175 ben 0 48 30 160 ben 7 62 32 159 ben 15 57 29 156 carol 0 39 30 136 carol 7 50 27 136 carol 15 63 24 134 ; run; data want; set have; by subject notsorted; length char_time $ 10; array x{*} _time var1-var800; array y{*} time heat -- weight ; retain _time var1-var800; if first.subject then do; do i=1 to dim(y); x{i}=y{i}; end; end; else do; char_time=catx('/',time,_time); do i=2 to dim(y); y{i}=y{i}-x{i}; end; output; end; drop i time _time var:; run;
Xia Keshan
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.