DATA Step, Macro, Functions and more

How to subtract list of variables between two time points

Accepted Solution Solved
Reply
Contributor
Posts: 25
Accepted Solution

How to subtract list of variables between two time points

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

subjectstimeheatspeedweight
jack06020179
jack75025179
jack156023175
ben 04830160
ben 76232159
ben 155729156
carol03930136
carol75027136
carol156324134

I want table like this

subjectstimeheatspeedweight
jack06020179
jack75025179
jack156023175
ben 04830160
ben 76232159
ben 155729156
carol03930136
carol75027136
carol156324134
jack_7/0-10-50
jack_15/00-3-4
ben _7/0142-1
ben _15/09-1-4

Accepted Solutions
Solution
‎02-11-2015 06:18 PM
Respected Advisor
Posts: 4,937

Re: How to subtract list of variables between two time points

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

PG

View solution in original post


All Replies
Super User
Posts: 11,343

Re: How to subtract list of variables between two time points

This line:

jack_7/0-10-50

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.

Contributor
Posts: 25

Re: How to subtract list of variables between two time points

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!

Solution
‎02-11-2015 06:18 PM
Respected Advisor
Posts: 4,937

Re: How to subtract list of variables between two time points

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

PG
Contributor
Posts: 25

Re: How to subtract list of variables between two time points

Thank You!

Super User
Posts: 10,048

Re: How to subtract list of variables between two time points

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 338 views
  • 0 likes
  • 4 in conversation