<?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: conditional sum in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/conditional-sum/m-p/856675#M338481</link>
    <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;Here it is. The variables names are different; also I added studentid since i want it per student.&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
create table sci_me_gr05 as
select distinct a.*,b.Item_Type, b.interactionTypes, b.ItemName
from  sg.sci_me_gr05  a left join TM.metadata   b
on a.itemuin =b.itemreference;
quit;

data sci_me_gr05;
	length clusterid $100.;
	set sci_me_gr05;

	if scan(lowcase(itemname),2,"-")="tao21" then clusterid=UPCASE(scan(itemname,3,"_"));
	else if substr(lowcase(itemname),1,2)="sc" then clusterid=UPCASE(scan(itemname,3,"_"));
	else if substr(lowcase(itemname),1,2)="nm" and index(itemname,"_") then clusterid=UPCASE(scan(itemname,2,"_"));
	else clusterid=UPCASE(scan(itemname,4,"-"));

	if index(lowcase(clusterid),"item") then clusterid=scan(clusterid,1,"_ ");
	if missing(clusterid) then clusterid=UPCASE(scan(scan(itemname,2,"_"),1," "));
	if clusterid="ITEM1" then clusterid=UPCASE(scan(scan(itemname,4,"-"),1,"_"));
run;

proc sort data=sci_me_gr05;
by StudentSystemUIN;
run;
proc freq data=sci_me_gr05;
    tables clusterid*itemstatus/noprint out=_counts_;
	by StudentSystemUIN;
run;
proc transpose data=_counts_ out=_counts_t;
    by clusterid;
    var count;
    id itemstatus;
run;


proc sql;
    create table CTT1 as select a.*
        ,case when b.Field_Test&amp;gt;0 and b.Operational&amp;gt;0 then sum(a.itemscore*(a.itemstatus='Operational')) 
            when b.Field_Test&amp;gt;0 and b.Operational&amp;lt;1 then sum(a.itemscore*(a.itemstatus='Field Test')) end as sum
        ,count(a.clusterid) as n

        from sci_me_gr05 as a left join _counts_t as b
        on a.clusterid=b.clusterid
		group by a.clusterid, a.StudentSystemUIN;
quit; &lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 01 Feb 2023 15:53:37 GMT</pubDate>
    <dc:creator>dustychair</dc:creator>
    <dc:date>2023-02-01T15:53:37Z</dc:date>
    <item>
      <title>conditional sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-sum/m-p/856177#M338316</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;I have a data set as below. I am trying to get the sum of scores within each cluster based on some rules:&lt;/P&gt;
&lt;P&gt;1. Within the same cluster if there are OI and FT items then sum only OI items' scores.&lt;/P&gt;
&lt;P&gt;2. If there are only FT items, then sum all of the FT items' scores.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also need the frequency of clusterids. I was kind of able to get the frequency and sum of OI items when there are only OI items, but it looks complicated to me now. Will appreciate for any help.&lt;/P&gt;
&lt;P&gt;Have&lt;/P&gt;
&lt;P&gt;clusterid item_type score&lt;/P&gt;
&lt;P&gt;AA11 OI 1&lt;/P&gt;
&lt;P&gt;AA11 FT 1&lt;/P&gt;
&lt;P&gt;AA11 OI 1&lt;/P&gt;
&lt;P&gt;AA22 FT 1&lt;/P&gt;
&lt;P&gt;AA22 FT 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Want&lt;/P&gt;
&lt;P&gt;clusterid item_type score sum Frequency&lt;/P&gt;
&lt;P&gt;AA11 OI 1 2 3&lt;/P&gt;
&lt;P&gt;AA11 FT 1 2 3&lt;/P&gt;
&lt;P&gt;AA11 OI 1 2 3&lt;/P&gt;
&lt;P&gt;AA22 FT 1 3 2&lt;/P&gt;
&lt;P&gt;AA22 FT 2 3 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jan 2023 05:15:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-sum/m-p/856177#M338316</guid>
      <dc:creator>dustychair</dc:creator>
      <dc:date>2023-01-30T05:15:45Z</dc:date>
    </item>
    <item>
      <title>Re: conditional sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-sum/m-p/856180#M338318</link>
      <description>&lt;P&gt;What have you tried?&lt;/P&gt;
&lt;P&gt;The problem can be solved by using DOW-loops, other ways exist, of course, using multiple steps.&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want_dow;
   if 0 then set have; /* maintain variable order */
   
   freq = 0;
   oi = 0;
   ft = 0;
   
   do _n_ = 1 by 1 until(last.clusterid);
      set have;
      by clusterid;
      
      freq = freq + 1;      
      ft = ft + ifn(item_type = 'FT', score, 0);
      oi = oi + ifn(item_type = 'OI', score, 0);
   end;
   
   if oi &amp;gt; 0 then sum = oi;
   else sum = ft;
   
   do _n_ = 1 by 1 until(last.clusterid);
      set have;
      by clusterid;
      output;
   end;
   
   drop ft oi;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;</description>
      <pubDate>Mon, 30 Jan 2023 06:44:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-sum/m-p/856180#M338318</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2023-01-30T06:44:57Z</dc:date>
    </item>
    <item>
      <title>Re: conditional sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-sum/m-p/856207#M338326</link>
      <description>&lt;PRE&gt;/*
Assuming there are only two levels (OI FI) in item_type.
*/
data have;
input clusterid $ item_type $ score;
cards;
AA11 OI 1
AA11 FT 1
AA11 OI 1
AA22 FT 1
AA22 FT 2
;
proc sql;
create table temp as
select *,case 
when sum(item_type='OI') and sum(item_type='FT') and item_type='OI' then score
when  sum(item_type='FT')=count(item_type) then score
else 0 end as temp,
count(item_type) as Frequency
 from have
  group by clusterid ;

create table want(drop=temp) as
select *,sum(temp) as sum
 from temp
  group by clusterid ;
quit;&lt;/PRE&gt;</description>
      <pubDate>Mon, 30 Jan 2023 10:39:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-sum/m-p/856207#M338326</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-01-30T10:39:56Z</dc:date>
    </item>
    <item>
      <title>Re: conditional sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-sum/m-p/856228#M338329</link>
      <description>&lt;P&gt;Here's my solution. One of the things I like about this is that you can see the intermediate results (from PROC FREQ and PROC TRANSPOSE) and so you can better understand the underlying logic. Yes, of course, you can do the whole thing in one DATA step (&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;)&amp;nbsp;or one SQL (&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;), but for beginners in SAS, I like this better.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note I have created data set named HAVE as SAS data step code. &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/22174"&gt;@dustychair&lt;/a&gt;: Please follow this example in future questions, provide data as &lt;FONT color="#FF0000"&gt;working&lt;/FONT&gt; SAS data step code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input clusterid $ item_type $ score;
cards;
AA11 OI 1
AA11 FT 1
AA11 OI 1
AA22 FT 1
AA22 FT 2
;
proc freq data=have;
    tables clusterid*item_type/noprint out=_counts_;
run;
proc transpose data=_counts_ out=_counts_t;
    by clusterid;
    var count;
    id item_type;
run;

proc sql;
    create table want as select a.*
        ,case when b.ft&amp;gt;0 and b.oi&amp;gt;0 then sum(a.score*(a.item_type='OI')) 
            when b.ft&amp;gt;0 and b.oi&amp;lt;1 then sum(a.score*(a.item_type='FT')) end as sum
        ,count(a.clusterid) as n

        from have as a left join _counts_t as b
        on a.clusterid=b.clusterid
        group by a.clusterid;
quit;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jan 2023 13:47:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-sum/m-p/856228#M338329</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-01-30T13:47:54Z</dc:date>
    </item>
    <item>
      <title>Re: conditional sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-sum/m-p/856585#M338451</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;Thank you for providing a possible solution. Some results are not as I expected but we are very close. It is working when there is a cluster that has only FT or only OI. However, when there are FT and OI items together in the same cluster it is not working for some cases. Please see&amp;nbsp; a small data from the results. According to this data the sum should be 0 for all rows however it is 2. Is it because FT=2?&lt;/P&gt;
&lt;P&gt;clusterid item_type score sum&lt;/P&gt;
&lt;P&gt;s2224 FT 2 2&lt;/P&gt;
&lt;P&gt;s2224 OI 0 2&lt;/P&gt;
&lt;P&gt;s2224 OI 0 2&lt;/P&gt;
&lt;P&gt;s2224 OI 0 2&lt;/P&gt;
&lt;P&gt;s2224 FT 0 2&lt;/P&gt;</description>
      <pubDate>Wed, 01 Feb 2023 06:08:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-sum/m-p/856585#M338451</guid>
      <dc:creator>dustychair</dc:creator>
      <dc:date>2023-02-01T06:08:33Z</dc:date>
    </item>
    <item>
      <title>Re: conditional sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-sum/m-p/856586#M338452</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;Thank you for providing a possible solution. I like the idea of being simple. I tried your code. The sum column has results only for FT items the rest is missing. I don't know what part is not working correctly.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Feb 2023 06:13:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-sum/m-p/856586#M338452</guid>
      <dc:creator>dustychair</dc:creator>
      <dc:date>2023-02-01T06:13:00Z</dc:date>
    </item>
    <item>
      <title>Re: conditional sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-sum/m-p/856609#M338462</link>
      <description>&lt;P&gt;Show us the code you used, or better yet the log. Don't ever say "it isn't working" without providing the code or the log.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Feb 2023 11:28:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-sum/m-p/856609#M338462</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-02-01T11:28:11Z</dc:date>
    </item>
    <item>
      <title>Re: conditional sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-sum/m-p/856675#M338481</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;Here it is. The variables names are different; also I added studentid since i want it per student.&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
create table sci_me_gr05 as
select distinct a.*,b.Item_Type, b.interactionTypes, b.ItemName
from  sg.sci_me_gr05  a left join TM.metadata   b
on a.itemuin =b.itemreference;
quit;

data sci_me_gr05;
	length clusterid $100.;
	set sci_me_gr05;

	if scan(lowcase(itemname),2,"-")="tao21" then clusterid=UPCASE(scan(itemname,3,"_"));
	else if substr(lowcase(itemname),1,2)="sc" then clusterid=UPCASE(scan(itemname,3,"_"));
	else if substr(lowcase(itemname),1,2)="nm" and index(itemname,"_") then clusterid=UPCASE(scan(itemname,2,"_"));
	else clusterid=UPCASE(scan(itemname,4,"-"));

	if index(lowcase(clusterid),"item") then clusterid=scan(clusterid,1,"_ ");
	if missing(clusterid) then clusterid=UPCASE(scan(scan(itemname,2,"_"),1," "));
	if clusterid="ITEM1" then clusterid=UPCASE(scan(scan(itemname,4,"-"),1,"_"));
run;

proc sort data=sci_me_gr05;
by StudentSystemUIN;
run;
proc freq data=sci_me_gr05;
    tables clusterid*itemstatus/noprint out=_counts_;
	by StudentSystemUIN;
run;
proc transpose data=_counts_ out=_counts_t;
    by clusterid;
    var count;
    id itemstatus;
run;


proc sql;
    create table CTT1 as select a.*
        ,case when b.Field_Test&amp;gt;0 and b.Operational&amp;gt;0 then sum(a.itemscore*(a.itemstatus='Operational')) 
            when b.Field_Test&amp;gt;0 and b.Operational&amp;lt;1 then sum(a.itemscore*(a.itemstatus='Field Test')) end as sum
        ,count(a.clusterid) as n

        from sci_me_gr05 as a left join _counts_t as b
        on a.clusterid=b.clusterid
		group by a.clusterid, a.StudentSystemUIN;
quit; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Feb 2023 15:53:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-sum/m-p/856675#M338481</guid>
      <dc:creator>dustychair</dc:creator>
      <dc:date>2023-02-01T15:53:37Z</dc:date>
    </item>
    <item>
      <title>Re: conditional sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-sum/m-p/856697#M338487</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;,&lt;/P&gt;
&lt;P&gt;I just added one statement to your code. it resolved those missing sums.&lt;/P&gt;
&lt;P&gt;thank you so much.&lt;/P&gt;
&lt;P&gt;Here is the final version of the code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
    create table CTT1 as select a.*
        ,case when b.Field_Test&amp;gt;0 and b.Operational&amp;gt;0 then sum(a.itemscore*(a.itemstatus='Operational')) 
         when b.Operational&amp;gt;0 then sum(a.itemscore*(a.itemstatus='Operational')) 
 when b.Field_Test&amp;gt;0 and b.Operational&amp;lt;1 then sum(a.itemscore*(a.itemstatus='Field Test')) end as sum
        ,count(a.clusterid) as n

        from sci_me_gr05 as a left join _counts_t as b
        on a.clusterid=b.clusterid
		group by a.clusterid, a.StudentSystemUIN;
quit; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Feb 2023 17:57:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-sum/m-p/856697#M338487</guid>
      <dc:creator>dustychair</dc:creator>
      <dc:date>2023-02-01T17:57:54Z</dc:date>
    </item>
  </channel>
</rss>

