<?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: Correctly use DO loop in SET statement to concatenate multiple datasets together in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Correctly-use-DO-loop-in-SET-statement-to-concatenate-multiple/m-p/502452#M134130</link>
    <description>I will save this code for future purposes..Looks nice..</description>
    <pubDate>Mon, 08 Oct 2018 16:59:32 GMT</pubDate>
    <dc:creator>rosegarden81</dc:creator>
    <dc:date>2018-10-08T16:59:32Z</dc:date>
    <item>
      <title>Correctly use DO loop in SET statement to concatenate multiple datasets together</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Correctly-use-DO-loop-in-SET-statement-to-concatenate-multiple/m-p/502077#M133969</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a path where there are 9 datasets (one for each year). I create counts of a common variable and create a separate data set that stores the counts. I am then trying to set all of these new count datasets into one dataset (without having to hard code). Below is an example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* Create counts from raw individual data files */&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table count_year2010 AS&lt;/P&gt;&lt;P&gt;SELECT count(patient) as CountPatient&lt;/P&gt;&lt;P&gt;FROM&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;year2010&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;QUIT;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table count_year2011 AS&lt;/P&gt;&lt;P&gt;SELECT count(patient) as CountPatient&lt;/P&gt;&lt;P&gt;FROM year2011;&lt;/P&gt;&lt;P&gt;QUIT;&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do the above for 9&amp;nbsp;datasets (using a macro) that creates these new datasets storing count.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My next step (if I were to hardcode would be):&lt;/P&gt;&lt;P&gt;data&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;set_hardcoded&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;set&amp;nbsp;&lt;/STRONG&gt;&lt;SPAN&gt;count_year2010&amp;nbsp;&amp;nbsp;count_year2011&amp;nbsp;count_year2012&amp;nbsp;count_year2013&amp;nbsp;count_year2014&amp;nbsp;count_year2015 ......&amp;nbsp;count_year2018;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;But instead of hardcoding all these individual datasets in the set statement, I am trying to find a way to use a DO loop such that I can append "_count" in the beginning of&amp;nbsp;year2010,&amp;nbsp;year2011 etc and use them in the set statement.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I tried the following:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%let inputdatasets =&amp;nbsp;year2010&amp;nbsp;year2011&amp;nbsp;year2012&amp;nbsp;year2013&amp;nbsp;year2014&amp;nbsp;year2015 year2016&amp;nbsp;year2017 year2018;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%let toappend=_count;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%macro test1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;data test;&amp;nbsp; /* in an attempt at creating the same result as&amp;nbsp;&lt;STRONG&gt;set_hardcoded&amp;nbsp;&lt;/STRONG&gt;above produces */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%do i = 1 to 9;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%let eachdata = %scan(&amp;amp;inputdatasets&amp;nbsp;, &amp;amp;i);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%let cat_var = %sysfunc(CATS(&amp;amp;toappend, &amp;amp;eachdata));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;set &amp;amp;cat_var&amp;nbsp;;&amp;nbsp; /* this does not work ; it only takes the last variable in inputdatasets&amp;nbsp;???*/&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%mend;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%temp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The above is not working and I am unable to simulate the&amp;nbsp;want portion. Any ideas on how to approach this would be greatly appreciated!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Regards&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 06 Oct 2018 01:44:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Correctly-use-DO-loop-in-SET-statement-to-concatenate-multiple/m-p/502077#M133969</guid>
      <dc:creator>rosegarden81</dc:creator>
      <dc:date>2018-10-06T01:44:44Z</dc:date>
    </item>
    <item>
      <title>Re: Correctly use DO loop in SET statement to concatenate multiple datasets together</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Correctly-use-DO-loop-in-SET-statement-to-concatenate-multiple/m-p/502080#M133970</link>
      <description>&lt;P&gt;this is not a do loop but it works better. &amp;nbsp;&lt;img id="heart" class="emoticon emoticon-heart" src="https://communities.sas.com/i/smilies/16x16_heart.png" alt="Heart" title="Heart" /&gt;&amp;nbsp; change the count_year to the name of the datasets that you have.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;		
	/* get all the datafile names into a macro var */
	select 		
		trim(libname) || '.' || memname into :dataset_vars separated by ' '
	from dictionary.tables
	where upcase(libname)="WORK"
	and upcase(memname) like 'COUNT_YEAR%' ;
quit;

data Count_years;
	set &amp;amp;dataset_vars;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;You will find this piece of code to be of great value in your future.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 06 Oct 2018 02:04:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Correctly-use-DO-loop-in-SET-statement-to-concatenate-multiple/m-p/502080#M133970</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2018-10-06T02:04:18Z</dc:date>
    </item>
    <item>
      <title>Re: Correctly use DO loop in SET statement to concatenate multiple datasets together</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Correctly-use-DO-loop-in-SET-statement-to-concatenate-multiple/m-p/502082#M133972</link>
      <description>&lt;P&gt;Looks like you are doing a lot of work for a simple task.&amp;nbsp; To combine datasets that are named with a numeric suffix just use a member name list.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set count_year2010 - count_year2018 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you did need to use macro %DO loop then make sure to use it to only generate the list of dataset names, and NOT generate the SET or semicolon.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set
%do yr=2010 %to 2018 ;
   count_year&amp;amp;yr
%end;
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 06 Oct 2018 02:07:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Correctly-use-DO-loop-in-SET-statement-to-concatenate-multiple/m-p/502082#M133972</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-10-06T02:07:48Z</dc:date>
    </item>
    <item>
      <title>Re: Correctly use DO loop in SET statement to concatenate multiple datasets together</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Correctly-use-DO-loop-in-SET-statement-to-concatenate-multiple/m-p/502083#M133973</link>
      <description>&lt;P&gt;Instead of complicating this with macro language, you could simply use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data not_hard_coded;&lt;/P&gt;
&lt;P&gt;set count_year20: ;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That will read all data set names that start with "count_year20"&lt;/P&gt;</description>
      <pubDate>Sat, 06 Oct 2018 02:07:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Correctly-use-DO-loop-in-SET-statement-to-concatenate-multiple/m-p/502083#M133973</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-10-06T02:07:39Z</dc:date>
    </item>
    <item>
      <title>Re: Correctly use DO loop in SET statement to concatenate multiple datasets together</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Correctly-use-DO-loop-in-SET-statement-to-concatenate-multiple/m-p/502084#M133974</link>
      <description>&lt;P&gt;You don't need a loop, use a naming convention and you can use the colon short cut to reference all at once.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd probably recommend possibly doing this at the beginning and using by group processing instead of macros. Otherwise your code will all be macros.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set count_year: ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you can back up a few steps, create a view which is the combination of all the data and then do the SQL or PROC MEANS.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The view is only called when you run the proc means so it doesn't take up space or anything and if your data sizes aren't super large this is more efficient.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data demo / view=demo;
set year2010-year2018 indsname=source;
dsn=source;
run;

proc sql;
create table summary_counts as
select dsn, count(patient) as CountPatient, 
from demo
group by dsn;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 06 Oct 2018 02:11:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Correctly-use-DO-loop-in-SET-statement-to-concatenate-multiple/m-p/502084#M133974</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-10-06T02:11:21Z</dc:date>
    </item>
    <item>
      <title>Re: Correctly use DO loop in SET statement to concatenate multiple datasets together</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Correctly-use-DO-loop-in-SET-statement-to-concatenate-multiple/m-p/502449#M134128</link>
      <description>&lt;P&gt;Thank you so much for this! I was able to implement this code per your recommendation..&lt;/P&gt;</description>
      <pubDate>Mon, 08 Oct 2018 16:57:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Correctly-use-DO-loop-in-SET-statement-to-concatenate-multiple/m-p/502449#M134128</guid>
      <dc:creator>rosegarden81</dc:creator>
      <dc:date>2018-10-08T16:57:46Z</dc:date>
    </item>
    <item>
      <title>Re: Correctly use DO loop in SET statement to concatenate multiple datasets together</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Correctly-use-DO-loop-in-SET-statement-to-concatenate-multiple/m-p/502452#M134130</link>
      <description>I will save this code for future purposes..Looks nice..</description>
      <pubDate>Mon, 08 Oct 2018 16:59:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Correctly-use-DO-loop-in-SET-statement-to-concatenate-multiple/m-p/502452#M134130</guid>
      <dc:creator>rosegarden81</dc:creator>
      <dc:date>2018-10-08T16:59:32Z</dc:date>
    </item>
  </channel>
</rss>

