<?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: Group and create new variable using different inputs per group in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Group-and-create-new-variable-using-different-inputs-per-group/m-p/639039#M21638</link>
    <description>In the end I found an ugly work-around:&lt;BR /&gt;&lt;BR /&gt;data intermediate;&lt;BR /&gt;set have;&lt;BR /&gt;if round=1 then days=r1days;&lt;BR /&gt;if round=2 then days=r2days;&lt;BR /&gt;if round=3 then days=r3days;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sort data=intermediate; by id drug round;run;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set intermediate;&lt;BR /&gt;by id, drug;&lt;BR /&gt;retain tot_drug_days;&lt;BR /&gt;if first.rxdrgnam then tot_drug_days = drug_days;&lt;BR /&gt;else tot_drug_days=tot_drug_days+drug-days; /*then the same for fills and pills*/&lt;BR /&gt;retain total_days;&lt;BR /&gt;if first.rxdrgnam then tot_days = days;&lt;BR /&gt;else tot_days=tot_days+days;&lt;BR /&gt;percent=tot_drug_days/tot_days;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Thanks for all your help - really need to learn more about arrays...</description>
    <pubDate>Fri, 10 Apr 2020 18:13:47 GMT</pubDate>
    <dc:creator>AliRKM</dc:creator>
    <dc:date>2020-04-10T18:13:47Z</dc:date>
    <item>
      <title>Group and create new variable using different inputs per group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Group-and-create-new-variable-using-different-inputs-per-group/m-p/638980#M21625</link>
      <description>&lt;P&gt;Hi clever people,&lt;BR /&gt;&lt;BR /&gt;I'm very new to SAS and am struggling to even think of the logic needed to program what I want.&lt;BR /&gt;I'm using SAS EG 7.15&lt;BR /&gt;My data consists of prescription refills for people purchased over 3 'rounds'. I have already grouped the drugs per round and summed the variables I need.&lt;BR /&gt;The rounds are of varying lengths and not all drugs are purchased in all rounds.&lt;BR /&gt;I need a sum of days in the 'round' but only if a drug was purchased in the round.&lt;BR /&gt;&lt;BR /&gt;Hard to explain - hopefully this will help:&lt;BR /&gt;data WORK.have;&lt;BR /&gt;infile datalines dsd truncover;&lt;BR /&gt;input ID:32. drug:$12. round:32. r1_days:32. r2_days:32. r3_days:32. drug_days:32. drug_fills:32. pills:32.;&lt;BR /&gt;datalines;&lt;BR /&gt;10027101 GLYBURIDE 1 60 153 304 90 3 360&lt;BR /&gt;10027101 GLYBURIDE 2 60 153 304 150 5 600&lt;BR /&gt;10027101 GLYBURIDE 3 60 153 304 120 4 480&lt;BR /&gt;10027101 METFORMIN 1 60 153 304 90 3 180&lt;BR /&gt;10027101 METFORMIN 2 60 153 304 150 5 300&lt;BR /&gt;10027101 METFORMIN 3 60 153 304 360 4 1440&lt;BR /&gt;10027101 SAXAGLIPTIN 2 60 153 304 90 1 90&lt;BR /&gt;10027101 SAXAGLIPTIN 3 60 153 304 90 1 90&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;data WORK.WANT;&lt;BR /&gt;infile datalines dsd truncover;&lt;BR /&gt;input id:32. drug:$12. tot_drug_days:32. Tot_drug_fills:32. Tot_pills:32. total_days:32. percent:32.;&lt;BR /&gt;datalines;&lt;BR /&gt;10027101 GLYBURIDE 360 12 1440 517 0.696324952&lt;BR /&gt;10027101 METFORMIN 600 12 1920 517 1.160541586&lt;BR /&gt;10027101 SAXAGLIPTIN 180 2 180 457 0.393873085&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;As you can see, SAXAGLIPTIN was purchased in rounds 2 and 3 so I only want to sum r2_days+r3_days whereas for other drugs I need to sum all 3 rounds.&lt;BR /&gt;&lt;BR /&gt;Many thanks.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Apr 2020 14:39:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Group-and-create-new-variable-using-different-inputs-per-group/m-p/638980#M21625</guid>
      <dc:creator>AliRKM</dc:creator>
      <dc:date>2020-04-10T14:39:34Z</dc:date>
    </item>
    <item>
      <title>Re: Group and create new variable using different inputs per group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Group-and-create-new-variable-using-different-inputs-per-group/m-p/638986#M21626</link>
      <description>&lt;P&gt;Do you need a report (that people read) or a data set?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would suggest providing an example of the data before you did what ever summary. Your creation of the exact same values of r1_days and such for all "rounds" is going to complicate things a lot because you include values that apparently should be summed.&lt;/P&gt;
&lt;P&gt;I think this may be doable without presummarizing but would need to see that data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is also a good idea to provide more than work case, such as another ID with a different values. Some solutions that may work for exactly one shown case may not work when applied to different data that has different combinations of present/missing data.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Apr 2020 14:55:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Group-and-create-new-variable-using-different-inputs-per-group/m-p/638986#M21626</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-10T14:55:52Z</dc:date>
    </item>
    <item>
      <title>Re: Group and create new variable using different inputs per group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Group-and-create-new-variable-using-different-inputs-per-group/m-p/638989#M21627</link>
      <description>&lt;P&gt;Please try the below code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID drug$ round  r1_days r2_days r3_days drug_days drug_fills pills;
cards;
1002710 GLYBURIDE 1 60 153 304 90 3 360
1002710 GLYBURIDE 2 60 153 304 150 5 600
1002710 GLYBURIDE 3 60 153 304 120 4 480
1002710 METFORMIN 1 60 153 304 90 3 180
1002710 METFORMIN 2 60 153 304 150 5 300
1002710 METFORMIN 3 60 153 304 360 4 1440
1002710 SAXAGLIPTIN 2 60 153 304 90 1 90
1002710 SAXAGLIPTIN 3 60 153 304 90 1 90
;

data want;
set have;
by id drug;
retain r1_days1 r2_days2 r3_days3;
array vars(*) r1_days r2_days r3_days;
array vars2(*) r1_days1 r2_days2 r3_days3;
do i = 1 to 3;
if first.drug then vars2(i)=.;
if round=i then vars2(i)=vars(i);
end;
total_daysx=sum(r1_days1,r2_days2, r3_days3);
run;

proc sort data=want;
by id drug descending round;
run;

data want2;
do until (last.drug);
set want;
by id drug descending round;
retain tot_drug_days Tot_drug_fills Tot_pills total_days;
if first.drug then tot_drug_days=drug_days;
else tot_drug_days+drug_days;

if first.drug then Tot_drug_fills=drug_fills;
else Tot_drug_fills+drug_fills;

if first.drug then Tot_pills=pills;
else Tot_pills+pills;

if first.drug then total_days=total_daysx;
if last.drug;
end;
do until (last.drug);
set want;
by id drug descending round;
percent=tot_drug_days/total_days;
keep id drug tot_drug_days Tot_drug_fills Tot_pills total_days percent;
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Apr 2020 15:32:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Group-and-create-new-variable-using-different-inputs-per-group/m-p/638989#M21627</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2020-04-10T15:32:16Z</dc:date>
    </item>
    <item>
      <title>Re: Group and create new variable using different inputs per group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Group-and-create-new-variable-using-different-inputs-per-group/m-p/638995#M21628</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/321983"&gt;@AliRKM&lt;/a&gt;&amp;nbsp; If I understand your description correctly, it's rather straight forward by group/array summarization.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input ID drug$ round  r1_days r2_days r3_days drug_days drug_fills pills;
cards;
1002710 GLYBURIDE 1 60 153 304 90 3 360
1002710 GLYBURIDE 2 60 153 304 150 5 600
1002710 GLYBURIDE 3 60 153 304 120 4 480
1002710 METFORMIN 1 60 153 304 90 3 180
1002710 METFORMIN 2 60 153 304 150 5 300
1002710 METFORMIN 3 60 153 304 360 4 1440
1002710 SAXAGLIPTIN 2 60 153 304 90 1 90
1002710 SAXAGLIPTIN 3 60 153 304 90 1 90
;

data want;
 do until(last.drug);
  set have;
  by id drug;
  array r r1_days r2_days r3_days;
  array q drug_days drug_fills pills;
  array t tot_drug_days tot_drug_fills tot_pills;
  do over q;
   t=sum(t,q);
  end;
  tot_days=sum(tot_days,r(round));
 end;
 percent=tot_drug_days/tot_days;
 keep id drug tot: percent; 
run;

proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT1" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;ID&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;drug&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;tot_drug_days&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;tot_drug_fills&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;tot_pills&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;tot_days&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;&lt;STRONG&gt;percent&lt;/STRONG&gt;&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1002710&lt;/TD&gt;
&lt;TD class="l data"&gt;GLYBURID&lt;/TD&gt;
&lt;TD class="r data"&gt;360&lt;/TD&gt;
&lt;TD class="r data"&gt;12&lt;/TD&gt;
&lt;TD class="r data"&gt;1440&lt;/TD&gt;
&lt;TD class="r data"&gt;517&lt;/TD&gt;
&lt;TD class="r data"&gt;0.69632&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1002710&lt;/TD&gt;
&lt;TD class="l data"&gt;METFORMI&lt;/TD&gt;
&lt;TD class="r data"&gt;600&lt;/TD&gt;
&lt;TD class="r data"&gt;12&lt;/TD&gt;
&lt;TD class="r data"&gt;1920&lt;/TD&gt;
&lt;TD class="r data"&gt;517&lt;/TD&gt;
&lt;TD class="r data"&gt;1.16054&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1002710&lt;/TD&gt;
&lt;TD class="l data"&gt;SAXAGLIP&lt;/TD&gt;
&lt;TD class="r data"&gt;180&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;180&lt;/TD&gt;
&lt;TD class="r data"&gt;457&lt;/TD&gt;
&lt;TD class="r data"&gt;0.39387&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Fri, 10 Apr 2020 16:24:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Group-and-create-new-variable-using-different-inputs-per-group/m-p/638995#M21628</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-04-10T16:24:36Z</dc:date>
    </item>
    <item>
      <title>Re: Group and create new variable using different inputs per group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Group-and-create-new-variable-using-different-inputs-per-group/m-p/639035#M21636</link>
      <description>Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;. That did not work over multiple ID's - as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; mentioned, I should have supplied a dataset with more variety&lt;BR /&gt;</description>
      <pubDate>Fri, 10 Apr 2020 18:05:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Group-and-create-new-variable-using-different-inputs-per-group/m-p/639035#M21636</guid>
      <dc:creator>AliRKM</dc:creator>
      <dc:date>2020-04-10T18:05:20Z</dc:date>
    </item>
    <item>
      <title>Re: Group and create new variable using different inputs per group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Group-and-create-new-variable-using-different-inputs-per-group/m-p/639036#M21637</link>
      <description>&lt;P&gt;Thanks Jag, that did not differentiate between drugs only being purchased in different rounds.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Apr 2020 18:06:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Group-and-create-new-variable-using-different-inputs-per-group/m-p/639036#M21637</guid>
      <dc:creator>AliRKM</dc:creator>
      <dc:date>2020-04-10T18:06:14Z</dc:date>
    </item>
    <item>
      <title>Re: Group and create new variable using different inputs per group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Group-and-create-new-variable-using-different-inputs-per-group/m-p/639039#M21638</link>
      <description>In the end I found an ugly work-around:&lt;BR /&gt;&lt;BR /&gt;data intermediate;&lt;BR /&gt;set have;&lt;BR /&gt;if round=1 then days=r1days;&lt;BR /&gt;if round=2 then days=r2days;&lt;BR /&gt;if round=3 then days=r3days;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sort data=intermediate; by id drug round;run;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set intermediate;&lt;BR /&gt;by id, drug;&lt;BR /&gt;retain tot_drug_days;&lt;BR /&gt;if first.rxdrgnam then tot_drug_days = drug_days;&lt;BR /&gt;else tot_drug_days=tot_drug_days+drug-days; /*then the same for fills and pills*/&lt;BR /&gt;retain total_days;&lt;BR /&gt;if first.rxdrgnam then tot_days = days;&lt;BR /&gt;else tot_days=tot_days+days;&lt;BR /&gt;percent=tot_drug_days/tot_days;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Thanks for all your help - really need to learn more about arrays...</description>
      <pubDate>Fri, 10 Apr 2020 18:13:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Group-and-create-new-variable-using-different-inputs-per-group/m-p/639039#M21638</guid>
      <dc:creator>AliRKM</dc:creator>
      <dc:date>2020-04-10T18:13:47Z</dc:date>
    </item>
    <item>
      <title>Re: Group and create new variable using different inputs per group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Group-and-create-new-variable-using-different-inputs-per-group/m-p/639041#M21639</link>
      <description>Thanks for your reply. I needed a dataset. Thanks for the tips - will include a more diverse data set next time.</description>
      <pubDate>Fri, 10 Apr 2020 18:15:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Group-and-create-new-variable-using-different-inputs-per-group/m-p/639041#M21639</guid>
      <dc:creator>AliRKM</dc:creator>
      <dc:date>2020-04-10T18:15:10Z</dc:date>
    </item>
  </channel>
</rss>

