BookmarkSubscribeRSS Feed
azt5173
Obsidian | Level 7

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. 

1 REPLY 1
andreas_lds
Jade | Level 19

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.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 744 views
  • 0 likes
  • 2 in conversation