<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Want the counts aggregately by visit wise. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868844#M343239</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Subject $ Visit $ Treatment $;
datalines;
100 Week1    TRTA
100 Week2   TRTA
100 Week3   TRTA
101 Week1   TRTB
102 Week1   TRTA
102 Week2  TRTA
102 Week3  TRTA
103 Week1 TRTB
103 Week2 TRTB
;

proc freq data=have noprint;
table Visit*Treatment /out=temp list nopercent nocol norow;
run;
proc transpose data=temp out=temp2(drop=_name_ _label_);
by visit;
var count;
id treatment;
run;
data want;
 set temp2;
 array a{99} _temporary_;
 array b{99} _temporary_;
offset_a=0;
offset_b=0;
do i=1 to _n_-1;
 offset_a+a{i};
 offset_b+b{i};
end;
trta=sum(trta,offset_a);
trtb=sum(trtb,offset_b);
a{i}=trta;
b{i}=trtb;
overall=sum(trta,trtb);
drop offset_: i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 10 Apr 2023 11:36:16 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2023-04-10T11:36:16Z</dc:date>
    <item>
      <title>Want the counts aggregately by visit wise.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868769#M343198</link>
      <description>&lt;P&gt;I have a data like below.&lt;/P&gt;&lt;P&gt;Subject Visit Treatment&lt;BR /&gt;100 Week1&amp;nbsp; &amp;nbsp; TRTA&lt;BR /&gt;100 Week2&amp;nbsp; &amp;nbsp;TRTA&lt;BR /&gt;100 Week3&amp;nbsp; &amp;nbsp;TRTA&lt;BR /&gt;101 Week1&amp;nbsp; &amp;nbsp;TRTB&lt;BR /&gt;102 Week1&amp;nbsp; &amp;nbsp;TRTA&lt;BR /&gt;102 Week2&amp;nbsp; TRTA&lt;BR /&gt;102 Week3&amp;nbsp; TRTA&lt;BR /&gt;103 Week1 TRTB&lt;BR /&gt;103 Week2 TRTB&lt;/P&gt;&lt;P&gt;i NEED THE SUBJECT COUNTS BASED ON TRAETMENT BY AGRAGATE BY VISIT WISE , SO THE DATA SHOULD LOOKS LIKE BELOW&lt;/P&gt;&lt;P&gt;VISIT TRTA TRTB OVERALL&lt;BR /&gt;Week1&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;BR /&gt;Week2&amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 7&lt;BR /&gt;Week3&amp;nbsp; &amp;nbsp; 8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;13&lt;/P&gt;&lt;P&gt;THE SUBJECTS COUNTS SHOULD BE SHOW AGRAGRATE BY VISIT AS SHOWN ABOVE&lt;/P&gt;</description>
      <pubDate>Sun, 09 Apr 2023 14:13:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868769#M343198</guid>
      <dc:creator>ambadi007</dc:creator>
      <dc:date>2023-04-09T14:13:44Z</dc:date>
    </item>
    <item>
      <title>Re: Want the counts aggregately by visit wise.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868770#M343199</link>
      <description>&lt;P&gt;Please do not type in ALL CAPITALS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have;
    tables visit*treatment/nocol norow nopercent;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 09 Apr 2023 14:35:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868770#M343199</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-04-09T14:35:56Z</dc:date>
    </item>
    <item>
      <title>Re: Want the counts aggregately by visit wise.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868771#M343200</link>
      <description>&lt;P&gt;hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;thanks for the reply, but as i mentioned i need to present like aggregate by each visit&amp;nbsp;&lt;/P&gt;&lt;P&gt;for example if one subject is participated in TRTA in Week1 and Week 2 then the count should be 2 in week2..like the below the counts are aggregating visit wise&amp;nbsp;&lt;/P&gt;&lt;P&gt;Vsit&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; TRTA&amp;nbsp; &amp;nbsp;TRTB&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Overall&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Week1&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Week2&amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 7&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Week3&amp;nbsp; &amp;nbsp; 8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;13&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 09 Apr 2023 14:42:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868771#M343200</guid>
      <dc:creator>ambadi007</dc:creator>
      <dc:date>2023-04-09T14:42:00Z</dc:date>
    </item>
    <item>
      <title>Re: Want the counts aggregately by visit wise.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868772#M343201</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76823"&gt;@ambadi007&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;as i mentioned i need to present like aggregate by each visit&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for example if one subject is participated in TRTA in Week1 and Week 2 then the count should be 2 in week2&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data aggregate;
    set have;
    by subject;
    if first.subject then aggregate=0;
    aggregate+1;
run;
proc freq data=aggregate;
    tables visit*aggregate/nocol nopercent;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 09 Apr 2023 14:47:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868772#M343201</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-04-09T14:47:22Z</dc:date>
    </item>
    <item>
      <title>Re: Want the counts aggregately by visit wise.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868781#M343210</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this is the counts I am getting after trying... i think the way i am saying is little confucing.. I have aded a new column "Needed."&lt;BR /&gt;Its needed if 1 subject is participated week1 and it is also participated in week 2 then week2 should show 2 subjects from week1 and 2 subjects from week2&lt;BR /&gt;and should show 4 as count ... hope now it is clear&lt;/P&gt;&lt;P&gt;vsit&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;aggregate&amp;nbsp; &amp;nbsp; &amp;nbsp; trt&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; COUNT&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Needed&lt;BR /&gt;Week1&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;TRTA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 (Partcipated in week1)&lt;BR /&gt;Week2&amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;TRTA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4 (Week2 +Week1 count)&lt;BR /&gt;continue .......&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 09 Apr 2023 15:22:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868781#M343210</guid>
      <dc:creator>ambadi007</dc:creator>
      <dc:date>2023-04-09T15:22:55Z</dc:date>
    </item>
    <item>
      <title>Re: Want the counts aggregately by visit wise.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868790#M343215</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76823"&gt;@ambadi007&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a data like below.&lt;/P&gt;
&lt;P&gt;Subject Visit Treatment&lt;BR /&gt;100 Week1&amp;nbsp; &amp;nbsp; TRTA&lt;BR /&gt;100 Week2&amp;nbsp; &amp;nbsp;TRTA&lt;BR /&gt;100 Week3&amp;nbsp; &amp;nbsp;TRTA&lt;BR /&gt;101 Week1&amp;nbsp; &amp;nbsp;TRTB&lt;BR /&gt;102 Week1&amp;nbsp; &amp;nbsp;TRTA&lt;BR /&gt;102 Week2&amp;nbsp; TRTA&lt;BR /&gt;102 Week3&amp;nbsp; TRTA&lt;BR /&gt;103 Week1 TRTB&lt;BR /&gt;103 Week2 TRTB&lt;/P&gt;
&lt;P&gt;i NEED THE SUBJECT COUNTS BASED ON TRAETMENT BY AGRAGATE BY VISIT WISE , SO THE DATA SHOULD LOOKS LIKE BELOW&lt;/P&gt;
&lt;P&gt;VISIT TRTA TRTB OVERALL&lt;BR /&gt;Week1&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;BR /&gt;Week2&amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 7&lt;BR /&gt;Week3&amp;nbsp; &amp;nbsp; 8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;13&lt;/P&gt;
&lt;P&gt;THE SUBJECTS COUNTS SHOULD BE SHOW AGRAGRATE BY VISIT AS SHOWN ABOVE&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please make sure that your "want" result can be made from the given example data.&lt;/P&gt;
&lt;P&gt;I see no way that you can arrive at 13 for an overall count given there are only 9 observations in the example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you mean: accumulate by Week and then aggregate across the week in a running total then show exactly which observations are counted for which output observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe we may also have suggested providing data in the form of data step code so we don't have to make guesses about your actual variable types or names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is my guess given the incomplete example data provided but it does match the desired output for the first two rows.&lt;/P&gt;
&lt;P&gt;Note that I add two variables to hold the running totals so you can actually examine if they are built in correctly. If so the you can rename them on the output data set.&lt;/P&gt;
&lt;PRE&gt;data have;
  input Subject $ Visit $ Treatment $;
datalines;
100 Week1    TRTA
100 Week2   TRTA
100 Week3   TRTA
101 Week1   TRTB
102 Week1   TRTA
102 Week2  TRTA
102 Week3  TRTA
103 Week1 TRTB
103 Week2 TRTB
;

proc freq data=have noprint;
   tables visit * treatment/out=firstcount(drop=percent);
run;

Proc transpose data=firstcount 
     out=trans (drop=_name_ _label)
;
   by visit;
   id treatment;
   var count;
run;

data runningtotal;
   set trans;
   retain r1 r2;
   r1= sum(r1,trta);
   r2= sum(r2,trtb);
   overall= sum(r1,r2);
run;
&lt;/PRE&gt;</description>
      <pubDate>Sun, 09 Apr 2023 16:58:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868790#M343215</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-04-09T16:58:15Z</dc:date>
    </item>
    <item>
      <title>Re: Want the counts aggregately by visit wise.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868802#M343222</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76823"&gt;@ambadi007&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is another potential solution&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Subject $ Visit $ Treatment $;
datalines;
100 Week1    TRTA
100 Week2   TRTA
100 Week3   TRTA
101 Week1   TRTB
102 Week1   TRTA
102 Week2  TRTA
102 Week3  TRTA
103 Week1 TRTB
103 Week2 TRTB
;
run;

/* Sort the data to support the want layout */
Proc sort data=have;
	by visit Treatment;
Run;

/* Store the unique treatment values in a space delimited macro variable */
proc sql noprint;
	select distinct Treatment
	into :treats separated by ' '
	from have;
quit;

/* Store the count of unique treatment values */
%let t_cnt = &amp;amp;sqlobs;

/* Generate accumulative totals */
data want(KEEP=visit &amp;amp;treats OVERALL);
	if 0 then set have;
	ARRAY trts {&amp;amp;t_cnt} 3 &amp;amp;treats;
	Retain &amp;amp;treats;
	
	do until (eof);
		SET have end=eof;
		By visit treatment;

		/* Increment the count of treatment variable as needed */
		do i=1 to dim(trts);
			trts[i]+ifc(treatment=vname(trts[i]),1,0) ;
		end;
		
		if (last.visit) then
		do;
			OVERALL = sum(of trts(*));
			output;
		end;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps&lt;/P&gt;</description>
      <pubDate>Sun, 09 Apr 2023 20:10:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868802#M343222</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2023-04-09T20:10:17Z</dc:date>
    </item>
    <item>
      <title>Re: Want the counts aggregately by visit wise.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868816#M343229</link>
      <description>&lt;P&gt;Hi Thanks for the reply, I was getting the below results while runnig the code,&lt;/P&gt;&lt;P&gt;Visit&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; TRTA TRTB OVERALL&lt;BR /&gt;Week1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;BR /&gt;Week2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;BR /&gt;Week3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;STRONG&gt;&amp;nbsp; &amp;nbsp;6(Week1+Week2+Week3.so this should be 8 )&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Basically I need the results like count of subjects present in Week1+Week2+Week3 and so on .&lt;BR /&gt;so I have edited the needed results in TRTA column 2 from week1 and 4 from week2, and the week3 &lt;STRONG&gt;count is 2 so the count for&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;week3 should be 8 . could you please help me&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Apr 2023 02:38:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868816#M343229</guid>
      <dc:creator>ambadi007</dc:creator>
      <dc:date>2023-04-10T02:38:27Z</dc:date>
    </item>
    <item>
      <title>Re: Want the counts aggregately by visit wise.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868835#M343237</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76823"&gt;@ambadi007&lt;/a&gt;&lt;BR /&gt;Week3 - TRTA = 6 is the correct accumulative count. While 8 would mean you have double counted TRTA somewhere!&lt;BR /&gt;The sample data set only has 6 entries for TRTA.</description>
      <pubDate>Mon, 10 Apr 2023 09:55:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868835#M343237</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2023-04-10T09:55:36Z</dc:date>
    </item>
    <item>
      <title>Re: Want the counts aggregately by visit wise.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868844#M343239</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Subject $ Visit $ Treatment $;
datalines;
100 Week1    TRTA
100 Week2   TRTA
100 Week3   TRTA
101 Week1   TRTB
102 Week1   TRTA
102 Week2  TRTA
102 Week3  TRTA
103 Week1 TRTB
103 Week2 TRTB
;

proc freq data=have noprint;
table Visit*Treatment /out=temp list nopercent nocol norow;
run;
proc transpose data=temp out=temp2(drop=_name_ _label_);
by visit;
var count;
id treatment;
run;
data want;
 set temp2;
 array a{99} _temporary_;
 array b{99} _temporary_;
offset_a=0;
offset_b=0;
do i=1 to _n_-1;
 offset_a+a{i};
 offset_b+b{i};
end;
trta=sum(trta,offset_a);
trtb=sum(trtb,offset_b);
a{i}=trta;
b{i}=trtb;
overall=sum(trta,trtb);
drop offset_: i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 10 Apr 2023 11:36:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868844#M343239</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-04-10T11:36:16Z</dc:date>
    </item>
    <item>
      <title>Re: Want the counts aggregately by visit wise.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868903#M343257</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76823"&gt;@ambadi007&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;for the inspiration , here is a solution that supports more than the two treatments (TRTA, TRTB) in the Sample (have) data.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Subject $ Visit $ Treatment $;
datalines;
100 Week1    TRTA
100 Week2   TRTA
100 Week3   TRTA
101 Week1   TRTB
102 Week1   TRTA
102 Week2  TRTA
102 Week3  TRTA
103 Week1 TRTB
103 Week2 TRTB
;
run;

/* Aggregate Weekly visits counts by treatment */
proc summary data=have nway;
	class visit treatment;
	output out=temp(drop=_type_ rename=(_freq_=cnt));
run;

/* Convert treatment Row to treatment Columns */
proc transpose data=temp out=temp2(drop=_:);
	by visit;
	id treatment;
	var	cnt;
run;

/* Create macro variables that will be used to derive dynamic code */
proc sql noprint;
	select distinct strip(treatment)
		, catx('=',treatment,cats('_',treatment))
		, cats('_',treatment)
	into  :trts separated by ' '
		, :trts_rename separated by ' '
		, :_trts separated by ' '
	from temp;
quit;
%let trts_cnt=&amp;amp;sqlobs;

/* Create data set to be used by Hash Object */
data work.temp;
	SET temp2(drop=visit rename=(&amp;amp;trts_rename));
	rid+1;
run;

DATA want(KEEP=visit &amp;amp;trts overall);

/* Define the variable in the PDV */
	if (0) then set temp2 temp;

	/* Declare arrays to group variables */
	array trts_arr {&amp;amp;trts_cnt} &amp;amp;trts;   * actual counts; 
	array _trts_arr {&amp;amp;trts_cnt} &amp;amp;_trts; * accumulated counts;

	/* Populate the Hash Object and Hash Iterator */
	if (_n_=1) then
	do;
		dcl hash h(dataset:'work.temp',ordered:'a');
		h.definekey('rid');
		h.definedata(all:'yes'); 
		h.definedone();
		dcl hiter hi('h');
	end;

	SET temp2;

	/* Reset to the first item on the Hash */
	rc=hi.first();

	/* Calculate accumulated counts */
	do i=1 to (_n_ - 1) while(rc=0);
		do j=1 to dim(trts_arr);
			trts_arr[j] = sum(trts_arr[j],_trts_arr[j]);
		end;
		rc=hi.next();
	end;
	overall = sum(of trts_arr(*));

	/* Update the stored info in the Hash Object */
	do j=1 to dim(trts_arr);
		_trts_arr[j] = trts_arr[j]; 
	end;
	h.replace();
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps&lt;/P&gt;</description>
      <pubDate>Mon, 10 Apr 2023 18:09:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/868903#M343257</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2023-04-10T18:09:48Z</dc:date>
    </item>
    <item>
      <title>Re: Want the counts aggregately by visit wise.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/869099#M343314</link>
      <description>Thanks for the help .. this works fine</description>
      <pubDate>Tue, 11 Apr 2023 12:45:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-the-counts-aggregately-by-visit-wise/m-p/869099#M343314</guid>
      <dc:creator>ambadi007</dc:creator>
      <dc:date>2023-04-11T12:45:40Z</dc:date>
    </item>
  </channel>
</rss>

