<?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: create multiple data sets use loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/create-multiple-data-sets-use-loop/m-p/374945#M276374</link>
    <description>&lt;P&gt;You can do hierarchical sums in a data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dlm=',';
input id :$1. group :$12. subgroup :$16. value;
cards;
1,patient care,physical contact,2
1,patient care,physical contact,3
1,patient care,family contact,1
1,patient care,family contact,6
1,patient care,other,3
1,patient care,other,3
2,education,conference,2
2,education,conference,4
2,education,read,1
2,education,read,5
2,education,teaching,7
2,education,teaching,2
;
run;

data want (keep=id group subgroup value);
set have;
by group notsorted subgroup notsorted;
retain groupsum sum;
if first.group then groupsum = 0;
if first.subgroup then sum = 0;
groupsum + value;
sum + value;
if last.subgroup
then do;
  value = sum;
  output;
end;
if last.group
then do;
  subgroup = '';
  value = groupsum;
  output;
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you just want a report, proc report is perfectly capable of doing that.&lt;/P&gt;</description>
    <pubDate>Tue, 11 Jul 2017 13:29:02 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-07-11T13:29:02Z</dc:date>
    <item>
      <title>create multiple data sets use loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-multiple-data-sets-use-loop/m-p/374702#M276369</link>
      <description>&lt;P&gt;Hi -,&lt;/P&gt;&lt;P&gt;I want to create sub data sets from main data sets using loop, my data looks like this:&lt;/P&gt;&lt;P&gt;id &amp;nbsp;group &amp;nbsp; &amp;nbsp; &amp;nbsp; subgroup&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; patient care &amp;nbsp; physical contact&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; patient care &amp;nbsp; family contact&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; patient care &amp;nbsp; other&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; education &amp;nbsp; &amp;nbsp; &amp;nbsp;conference&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; education &amp;nbsp; &amp;nbsp; &amp;nbsp;read&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; education &amp;nbsp; &amp;nbsp; &amp;nbsp;teaching&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to sub set the data by each group and each subgroup:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data patient_care;
    set data;
     if group = 'patient care';
run;

data edu;
   set data;
   if group = 'Education';
run;

data patient_care1;
    set data;
    if subgroup = 'physical contact';
run;

data patient_care2;
   set data;
   if subgroup = 'family contact';
run;

data patient_care3;
   set data;
   if subgroup = 'other';
run;

data edu1;
   set data;
   if subgroup = 'conference';
run;

data edu2;
   set data;
   if subgroup = 'read';
run;

data edu3;
   set data;
   if subgroup  = 'teaching';
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The code is very redundant, I think looping will be helpful but not sure exactly what to do, any idea?&lt;/P&gt;&lt;P&gt;Thanks!!!&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jul 2017 20:38:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-multiple-data-sets-use-loop/m-p/374702#M276369</guid>
      <dc:creator>panda</dc:creator>
      <dc:date>2017-07-10T20:38:20Z</dc:date>
    </item>
    <item>
      <title>Re: create multiple data sets use loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-multiple-data-sets-use-loop/m-p/374705#M276370</link>
      <description>&lt;P&gt;This is rarely a good way to work with your data. Why do you want to split it in the first place?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FYI - the best answer to this question is usually don't do it. Because many people insist anyways see these posts:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.sas.com/content/sasdummy/2015/01/26/how-to-split-one-data-set-into-many/" target="_blank"&gt;http://blogs.sas.com/content/sasdummy/2015/01/26/how-to-split-one-data-set-into-many/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.sascommunity.org/wiki/Split_Data_into_Subsets" target="_blank"&gt;http://www.sascommunity.org/wiki/Split_Data_into_Subsets&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jul 2017 20:46:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-multiple-data-sets-use-loop/m-p/374705#M276370</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-10T20:46:04Z</dc:date>
    </item>
    <item>
      <title>Re: create multiple data sets use loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-multiple-data-sets-use-loop/m-p/374706#M276371</link>
      <description>&lt;P&gt;First would be to explain what you are doing that requires all of those sets. The request for many sets is usually immediately followed by questions on how to use them to do the same processing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could extend this logic:&lt;/P&gt;
&lt;PRE&gt;data patient_care
     edu
;
    set data;
     if group = 'patient care' then output Patient_care;
     if group = 'Education' then output edu;
run;&lt;/PRE&gt;
&lt;P&gt;each condition can send output to the specified output set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jul 2017 20:47:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-multiple-data-sets-use-loop/m-p/374706#M276371</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-07-10T20:47:43Z</dc:date>
    </item>
    <item>
      <title>Re: create multiple data sets use loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-multiple-data-sets-use-loop/m-p/374801#M276372</link>
      <description>&lt;P&gt;Put your control data in a dataset, and let a data step create the split code with call execute:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lookup;
infile cards dlm=',';
input id :$1. group :$12. subgroup :$16.;
cards;
1,patient care,physical contact
1,patient care,family contact
1,patient care,other
2,education,conference
2,education,read
2,education,teaching
;
run;

data _null_;
call execute('data');
do until (eof1);
  set lookup end=eof1;
  by id;
  if first.id then call execute(' ' !! translate(trim(group),'_',' '));
  call execute(' ' !! translate(trim(subgroup),'_',' '));
end;
call execute('; set data;');
do until (eof2);
  set lookup end=eof2;
  by id;
  if first.id then call execute('if group = "' !! trim(group) !! '" then output ' !! translate(trim(group),'_',' ') !! ';');
  call execute('if subgroup = "' !! trim(subgroup) !! '" then output ' !! translate(trim(subgroup),'_',' ') !! ';');
end;
call execute('run;');
stop;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that the created code will make just one pass through the original dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The question remains, what will you do with the resulting datasets? If you perform the same analytics/reports on all of them, you're better off doing it in one pass with by-group processing.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jul 2017 06:08:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-multiple-data-sets-use-loop/m-p/374801#M276372</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-07-11T06:08:42Z</dc:date>
    </item>
    <item>
      <title>Re: create multiple data sets use loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-multiple-data-sets-use-loop/m-p/374936#M276373</link>
      <description>&lt;P&gt;Hi -&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to split the data because I am not sure how to sum all observations across rows by group and subgroup and generate new observations as sums of those groups, right now I am using proc print and copy and paste the output in excel:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc print data = data;
var inter0-inter23;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The original data is like this, each id has one unique group:&lt;/P&gt;&lt;P&gt;id &amp;nbsp;group &amp;nbsp; &amp;nbsp;inter0 &amp;nbsp;inter1 &amp;nbsp;inter2 &amp;nbsp;inter3 &amp;nbsp; ... &amp;nbsp; inter23&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; patient care&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; education&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; patinet care&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp;education&lt;/P&gt;&lt;P&gt;each id has one unique subgroup:&lt;/P&gt;&lt;P&gt;id subgroup &amp;nbsp; &amp;nbsp; &amp;nbsp; inter0 inter1 inter2 inter3 ... inter23&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; patient&amp;nbsp;interaction&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; family interaction&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; conference&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp;read&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp;teaching&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp;patient interaction&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp;family interaction&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp;conference&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp;read&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; teaching&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to sum all ids by group and subgroup, the ideal ouput looks like this&lt;/P&gt;&lt;P&gt;group &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;inter0 &amp;nbsp; inter1 inter2 ..... inter23&lt;/P&gt;&lt;P&gt;patient care&lt;/P&gt;&lt;P&gt;education&lt;/P&gt;&lt;P&gt;subgroup&lt;/P&gt;&lt;P&gt;patient interaction&lt;/P&gt;&lt;P&gt;family interaction&lt;/P&gt;&lt;P&gt;conference&lt;/P&gt;&lt;P&gt;read&amp;nbsp;&lt;/P&gt;&lt;P&gt;teaching&lt;/P&gt;&lt;P&gt;Not sure how to do that, any idea?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jul 2017 13:19:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-multiple-data-sets-use-loop/m-p/374936#M276373</guid>
      <dc:creator>panda</dc:creator>
      <dc:date>2017-07-11T13:19:11Z</dc:date>
    </item>
    <item>
      <title>Re: create multiple data sets use loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-multiple-data-sets-use-loop/m-p/374945#M276374</link>
      <description>&lt;P&gt;You can do hierarchical sums in a data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dlm=',';
input id :$1. group :$12. subgroup :$16. value;
cards;
1,patient care,physical contact,2
1,patient care,physical contact,3
1,patient care,family contact,1
1,patient care,family contact,6
1,patient care,other,3
1,patient care,other,3
2,education,conference,2
2,education,conference,4
2,education,read,1
2,education,read,5
2,education,teaching,7
2,education,teaching,2
;
run;

data want (keep=id group subgroup value);
set have;
by group notsorted subgroup notsorted;
retain groupsum sum;
if first.group then groupsum = 0;
if first.subgroup then sum = 0;
groupsum + value;
sum + value;
if last.subgroup
then do;
  value = sum;
  output;
end;
if last.group
then do;
  subgroup = '';
  value = groupsum;
  output;
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you just want a report, proc report is perfectly capable of doing that.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jul 2017 13:29:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-multiple-data-sets-use-loop/m-p/374945#M276374</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-07-11T13:29:02Z</dc:date>
    </item>
    <item>
      <title>Re: create multiple data sets use loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-multiple-data-sets-use-loop/m-p/374965#M276375</link>
      <description>&lt;P&gt;Don't know why but notsorted did not work well, after I remove notsorted command, I can get the result I want (need to sort by group and subgroup first):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort have;&lt;BR /&gt;by group subgroup;&lt;BR /&gt;run;&lt;BR /&gt;
data want (keep=id group subgroup value);
set have;
by group subgroup;
retain groupsum sum;
if first.group then groupsum = 0;
if first.subgroup then sum = 0;
groupsum + value;
sum + value;
if last.subgroup
then do;
  value = sum;
  output;
end;
if last.group
then do;
  subgroup = '';
  value = groupsum;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Jul 2017 13:57:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-multiple-data-sets-use-loop/m-p/374965#M276375</guid>
      <dc:creator>panda</dc:creator>
      <dc:date>2017-07-11T13:57:22Z</dc:date>
    </item>
    <item>
      <title>Re: create multiple data sets use loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-multiple-data-sets-use-loop/m-p/374968#M276376</link>
      <description>&lt;P&gt;I used the notsorted option so that the code works when there is no ascending/descending order. One has to make sure that the groups come in blocks, though (no identical by values scattered across the dataset). If sorting does not interfere with other logic, then it is to be preferred.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jul 2017 14:00:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-multiple-data-sets-use-loop/m-p/374968#M276376</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-07-11T14:00:22Z</dc:date>
    </item>
  </channel>
</rss>

