DATA Step, Macro, Functions and more

loops to update allocation

Reply
Occasional Contributor
Posts: 15

loops to update allocation

I have a data that kind of looks like this: I have 2310 subjects, 3 Treatments(1,2,3) and The variable X which is X(Age factor, Treatment) so X12 would be age factor 1 and treatment 2. 

 

data have;
input  Subject  Treatment X;
datalines;
 1            1     X12
 2            2     X12
 3            3     X13
 4            1     X11
 5            2     X13
 6            3     X12
 7            1     X11
 8            2     X12
 9            1     X11
 10           3     X13
.......................
2310 3 X12 ;

I have to count the number of X's using the variable Z so Z11=#of X11's , Z12=#of X12's and so on but if the last number in the X and T is the same then you add one to the allocation.

So Z11=X11+1 if T=1, Z12=X12+1 If T=2 and Z13=X13+1 if T=3.  If last number of X and T don't correspond to each other then it would stay the same:

Z11=X11,if T=2 or T=3

Z12=X12 if T=2 or T=1

Z13=Z13. if T=1 or T=2

I also have to define a function where:

M1=std(Z11,Z12,Z13) where T=1 and Z11=Z11+1

M2=std(Z11,Z12,Z13) where T=2 and Z12=Z12+1

M3=std(Z11,Z12,Z13) where T=3 and Z13=Z13+1

 

In the end my result should look like this:

Z11 Z12 Z13 Z21 Z22 Z23 Z31 Z32 Z33

20   25   10   12    15  18    19  30    25

 

G1:263

G2:212

G3:258

 

So far I have this is the code I have:

data count;
set dynamic;
z11=0;
z12=0;
z13=0;
if Y="X11" then do;
z11=z11+1;
if T=1 then Z11=Z11+1;;
end;
else if Y="X12" then do;
z12=z12+1;
if T=2 then Z12=Z12+1;
end;
else if Y="X13" then do;
z13=z13+1;
if T=3 then z13=z13+1;
end;
run;

I am having trouble on how to figure out the function in the code above as I would need the counts of Z11 when T=1 and Z11 when T is not equal to 1. Any help would be appreciated. 

Valued Guide
Posts: 582

Re: loops to update allocation

Can you post the expected result of the data you posted?

 

Proc summary is my first choice, when things have to be counted. If i understood the rules you have to apply, then the z??-counter is increased if substr(x, 3, 1) = Treatment. That rule can be used in a where statement:

 

data have;
input Subject Treatment  X $;
datalines;
 1 1 X12
 2 2 X12
 3 3 X13
 4 1 X11
 5 2 X13
 6 3 X12
 7 1 X11
 8 2 X12
 9 1 X11
 10 3 X13
 ;
 run;

proc summary data=have(where=(put(Treatment, 1.) = substr(x, 3, 1))) nway;
   class x;
   output out=work.counted(drop= _type_ rename=(_freq_ = count));
run;

Things to do: change x to z in Varible x and transpose it.

Ask a Question
Discussion stats
  • 1 reply
  • 60 views
  • 0 likes
  • 2 in conversation