Performing same computation by strata

Accepted Solution Solved
Reply
Super Contributor
Posts: 292
Accepted Solution

Performing same computation by strata

Data one;

input censored time;

cards;

0 1

0 2

1 2

0 4

1 5

0 6

0 9

1 10

; run;

Data one; set one; ord=_N_;

run;

%let num=8;

proc transpose data=one out=t1;

ID ord; var time; run;

Data t1; set t1; rename _1-_&num=t1-t# run;

proc transpose data=one out=t2;

ID ord; var censored; run;

Data t2; set t2; rename _1-_&num=c1-c# run;

Data two; merge t1 t2; run;

data cal1;

set two; array t (8) t1-t#      array c (8) c1-c#    array v (8) v1-v#

do i=1 to &num-1;

v=0;

if c=0 then v=t;

if c=1 then do j=i+1 to # v=t+v; end;

end;

v[&num]=t[&num]; keep v1-v#

run;

Data te;

set cal1; array v (8) v1-v#

do i=1 to #

times=v; output; end; keep times; run;

Data te; set te; ord=_N_; run;

Data final; merge one te ; by ord; drop ord; run;

proc print data=final;

run;

1

0

1

1

2

0

2

2

3

1

2

34

4

0

4

4

5

1

5

25

6

0

6

6

7

0

9

9

8

1

10

10

How can I modify the code to do the same thing but by strata.

Thant’s

Data one;

input censored time strata;

cards;

0 1  1

0 2  1

1 2  1

0 4  1

1 5  1

0 6  1

0 9  1

1 10  1

0 1  2

0 2  2

1 2  2

0 4  2

1 5  2

0 6  2

0 9  2

1 10  2

Create same data as above but by strata.

How can I modify the above code to do this.

Very similar to the first case but this is by strata.

1

0

1

1     1

2

0

2

1     2

3

1

2

1   34

4

0

4

1    4

5

1

5

1   25

6

0

6

1     6

7

0

9

1     9

8

1

10

1    10

1

0

1

2    1

2

0

2

2     2

3

1

2

2    34

4

0

4

2    4

5

1

5

2   25

6

0

6

  2    6

7

0

9

2    9

8

1

10

2 10


Accepted Solutions
Solution
‎04-04-2015 07:36 PM
Grand Advisor
Posts: 17,447

Re: Performing same computation by strata

If you only have two strata's there's no point. Copy and paste the code twice and implement a WHERE clause on the data. Otherwise it's probably worth you trying it first.

ie

Data one;

set one;

WHERE STRATA=1;

ord=_N_;

run;


*rest of code;

*Save final dataset;

Data final1; merge one te ; by ord; drop ord; run;


Data one;

set one;

WHERE STRATA=2;

ord=_N_;

run;


*rest of code;

*Save final dataset;

Data final2; merge one te ; by ord; drop ord; run;

*Append datasets;


data final;set final1 final2;

run;


View solution in original post


All Replies
Grand Advisor
Posts: 17,447

Re: Performing same computation by strata

Either add in a bunch of BY statements to the relevant procs, using first/last to initialize arrays as needed

OR create a macro to run each strata at a time.

Super Contributor
Posts: 292

Re: Performing same computation by strata

I do not know how to do any, can you show me how to use a macro and by statement. I love macro and learning everyday

Solution
‎04-04-2015 07:36 PM
Grand Advisor
Posts: 17,447

Re: Performing same computation by strata

If you only have two strata's there's no point. Copy and paste the code twice and implement a WHERE clause on the data. Otherwise it's probably worth you trying it first.

ie

Data one;

set one;

WHERE STRATA=1;

ord=_N_;

run;


*rest of code;

*Save final dataset;

Data final1; merge one te ; by ord; drop ord; run;


Data one;

set one;

WHERE STRATA=2;

ord=_N_;

run;


*rest of code;

*Save final dataset;

Data final2; merge one te ; by ord; drop ord; run;

*Append datasets;


data final;set final1 final2;

run;


☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 185 views
  • 0 likes
  • 2 in conversation