<?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 programming (macro?) to identify &amp;amp; merge data sets? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditional-programming-macro-to-identify-amp-merge-data-sets/m-p/509574#M137004</link>
    <description>I rarely do this, and not sure I have time for it, but can you email me the full file via DM and I'll take a look? Include one or two variations and I can make some recommendations on how to refactor it for efficiency.</description>
    <pubDate>Thu, 01 Nov 2018 15:42:34 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-11-01T15:42:34Z</dc:date>
    <item>
      <title>Conditional programming (macro?) to identify &amp; merge data sets?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-programming-macro-to-identify-amp-merge-data-sets/m-p/509365#M136927</link>
      <description>&lt;P&gt;I need to write a program to identify and selectively merge SAS data sets.&amp;nbsp; I’m working with 13 years of survey data from 1999-2012 – each is a separate data set.&amp;nbsp; Complex survey data – each year of data has 100 strata containing 2 or 3 PSUs (grouped PSUs). &amp;nbsp;I’ve created separate replicate data sets for each PSU by strata (replicates for jackknife standard error estimation): have one for each of the strata having 2 PSUs and 2 for all strata having 3 PSUs. &amp;nbsp;The number of strata having 3 PSUs varies by year of data collection (range: 13-31 strata/year have 3 PSUs). The strata ID numbers (1-100) for those having 3 PSUs aren’t a continuous series. For example, for one year strata numbers 3, 7, 9, 13, 21, 22 and 37 might have 3 PSUs. The replicate data sets are indexed by the strata number. So for example rep2_3, rep2_7, rep2_9…. would identify the replicate data sets for my example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I now need to write syntax to identify and merge these replicate data sets for strata having 3 PSUs across years of observation by strata index number. For example, Strata # 5 (Rep2_5) might have had 3 PSUs and now corresponding replicate data sets for 1999, 2001, 2002, 2007 and 2011. I want to find the rep2_5 data sets in folders for their respective years and concatenate them. Is this a situation for %GOTO?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions as to how I can use some sort of conditional programming and indices to identify and concatenate replicate data sets for each strata ID across years? &amp;nbsp;Thank you so much for reading this! &amp;nbsp;I’m truly flummoxed!&lt;/P&gt;</description>
      <pubDate>Wed, 31 Oct 2018 22:11:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-programming-macro-to-identify-amp-merge-data-sets/m-p/509365#M136927</guid>
      <dc:creator>SharonZS</dc:creator>
      <dc:date>2018-10-31T22:11:50Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional programming (macro?) to identify &amp; merge data sets?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-programming-macro-to-identify-amp-merge-data-sets/m-p/509374#M136931</link>
      <description>&lt;P&gt;How does this data set get used in your analysis?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you know what the final data set should look like for what you are attempting? If so then provide some example of the actual variable names and values for a few replicates (3 or 4 should work), preferably as a data step for each and an example&amp;nbsp;what you want the final data set to look like using that example input.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Folders are not the issue if you build SAS data sets, LIBRARY names are.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If each of the data sets is structured the same combining them may be as simple as&lt;/P&gt;
&lt;PRE&gt;data allreps;
   set lib1.rep: lib2.rep: lib3.rep: ;
run;&lt;/PRE&gt;
&lt;P&gt;assuming each year's data is in a separate library. The above would combine all data sets in each of the 3 dummy libraries that begin with REP (that's what the colon character ending the data set name does, a wildcard list builder as it were).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course if you have other sets that start with rep that you don't want that might be an issue and the set statement gets more&lt;/P&gt;
&lt;P&gt;complicated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I am not sure of is why you built so many different data sets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your description goes into a lot of details that actually have little to do with selecting or merging. If there is data actually contained in the data set names, as implied by rep2_7 and such, I hope the corresponding information is stored in variables inside each set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One thing that might be an issue: are the meanings of the strata across years the same? Assuming geographical meaning does strata 1 always represent the same geography (or other subpopulation)&amp;nbsp;when it appears? If not you may need to do some additional adjustments on the strata variables.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Oct 2018 23:39:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-programming-macro-to-identify-amp-merge-data-sets/m-p/509374#M136931</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-10-31T23:39:32Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional programming (macro?) to identify &amp; merge data sets?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-programming-macro-to-identify-amp-merge-data-sets/m-p/509375#M136932</link>
      <description>When doing Ballardw suggestion of a few variations, can you also show what that would look like on your original data source? You may need a DoW loop but when I see a lot of subset data I start to smell issues and lots of places for errors.</description>
      <pubDate>Wed, 31 Oct 2018 23:45:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-programming-macro-to-identify-amp-merge-data-sets/m-p/509375#M136932</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-10-31T23:45:41Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional programming (macro?) to identify &amp; merge data sets?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-programming-macro-to-identify-amp-merge-data-sets/m-p/509385#M136940</link>
      <description>&lt;P&gt;Thank you so much for your responses! My apologies for any lack of clarity -- trying to be concise and missed! This is a long-standing project (10 year grant) and I'm revising and extending macros written by another statistician... Looking at changes in cost for 78 medical conditions from 1999-2012.&amp;nbsp; The data sets that I'm trying to identify and merge are actually separate data sets containing cost estimates, prevalence, and coefficient estimates for each replicate (1-100 for reps and the non-continuous index for strata which contain a third PSU) for each year of observation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm merging the cost, prevalence and beta estimates to create a merged set of data sets to examine the extent to which changes in medical spending are attributable to changes in the cost of treatment versus changes in the prevalence of the health condition over time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All of the replicates for each year are in separate SAS libraries by year: est99.replicate2_&amp;amp;j&amp;nbsp; &amp;nbsp;est00.replicate2_&amp;amp;j&lt;/P&gt;&lt;P&gt;The problem I have is that the second replicate for the strata having 3 PSUs&amp;nbsp; isn't consistent for all years. I somehow need to create indices for both year and strata number. I've used indices for variables and imputed data sets in the same library (er.g imputed data sets) but don't know how to do this for data sets across libraries.&amp;nbsp; I also don't know how to do this for a non-continuous index that changes across years....&amp;nbsp; First set&amp;nbsp; of replicates was very straightforward (do j =1 to 100).&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My existing syntax is run by strata number (other statistician didn't make it this far to deal with this...). For example, I want to identify and merge together all of the cost estimates for strata 3 for the years when this strata had 3 PSUs. I've done the easy case where the strata has 3 PSUs for all years.&amp;nbsp; I'm now trying to figure out how to do it for the remaining strata without doing incredibly laborious and error prone hard coding.... Existing code looked like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The names here are the macro call are the data sets containing means for each replicate (final_est_replicate2_&amp;amp;j for all 13 years)&amp;nbsp;&lt;/P&gt;&lt;P&gt;I use another macro to run this for all years and strata...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro cgar_from_year_a&lt;BR /&gt;(year=,rep=,master=,coeff=,cost=, name1=,name2=,name3=,name4=,name5=,name6=,name7=,......name13=&amp;nbsp;);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data final_estimates99;&lt;BR /&gt;set est99.&amp;amp;name1;&lt;BR /&gt;year = 1999;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data final_estimates00;&lt;BR /&gt;set est00.&amp;amp;name2;&lt;BR /&gt;year = 2000;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data final_estimates01;&lt;BR /&gt;set est01.&amp;amp;name3;&lt;BR /&gt;year = 2001;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Goes up to 2012. Problem now is that SAS of course stops when it encounters a year/library that doesn't contain a data set for that replicate (strata that doesn't have a third PSU for that year). The existing syntax concatenates all of the cost data sets:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Data attrib_cost_long;&lt;BR /&gt;set&amp;nbsp; final_estimates99&amp;nbsp; final_estimates00 final_estimates01&amp;nbsp; final_estimates02 final_estimates03&lt;BR /&gt;final_estimates04 final_estimates05&amp;nbsp; final_estimates06 final_estimates07&amp;nbsp; final_estimates08&lt;BR /&gt;final_estimates09 final_estimates10&amp;nbsp; final_estimates11&amp;nbsp; final_estimates12;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to figure out how to get something similar when the replicate data sets only exist for a subset of years of observation.&amp;nbsp;&lt;/P&gt;&lt;P&gt;At this point the only variable for number of PSUs for strata per year&amp;nbsp;exists in another data set that I made. The names of the data sets are really the only indicator as to whether there was a third PSU in that strata for the year (if rep2_&amp;amp;j exists in libraryYR).&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much for your responses!&amp;nbsp; I've been asking around at my institution and no one has any idea of what to do (I'm more of a modeler...).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Nov 2018 00:46:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-programming-macro-to-identify-amp-merge-data-sets/m-p/509385#M136940</guid>
      <dc:creator>SharonZS</dc:creator>
      <dc:date>2018-11-01T00:46:42Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional programming (macro?) to identify &amp; merge data sets?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-programming-macro-to-identify-amp-merge-data-sets/m-p/509387#M136942</link>
      <description>Yeah, you'd be better off refactoring this from scratch and get it working. I think he replicates are a problem. How many data sets do you have in total and how many rows in the data set if you were to combine it all together?</description>
      <pubDate>Thu, 01 Nov 2018 00:56:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-programming-macro-to-identify-amp-merge-data-sets/m-p/509387#M136942</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-01T00:56:31Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional programming (macro?) to identify &amp; merge data sets?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-programming-macro-to-identify-amp-merge-data-sets/m-p/509572#M137002</link>
      <description>&lt;P&gt;The replicates are definitely the problem! I have over 1600+ of them (other secret detail that I didn't even mention is that in 2012 they changed the PSU variable so I have 525 replicates for that year alone!!).&amp;nbsp; I may end up hard coding this...Ugh.. Thank you so much for your responses!&amp;nbsp; Seriously the most useful feedback I've gotten in weeks!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Couple thoughts I had after reading your responses. Is there a way to use the %GOTO or %RETURN to do this? For example, I've created variables for the number of PSUs in each stratum by year: psuYR equal to 2 or 3 for each stratum.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro combdat (psuYR);&lt;/P&gt;&lt;P&gt;%if &amp;amp;psuYR&amp;nbsp; ne 3 %then %return;&amp;nbsp; /*Only creates data set for strata having 3 PSUs*/&lt;/P&gt;&lt;P&gt;data final_estimatesYR;&lt;/P&gt;&lt;P&gt;set estYR.final_estimates_rep2_&amp;amp;j;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%mend combdat;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does this look like a direction worth pursuing? Would run for each year within each stratum number.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The other piece that I thought might be useful that I do have is a list of strata with 3 PSUs created for each year:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Helvetica',sans-serif; color: #333333;"&gt;data stratlist;&lt;BR /&gt;set strats;&lt;BR /&gt;by psu strat;&lt;BR /&gt;if psu = 3 then list = '&amp;amp;j='||trim(left(strat))||' or ';&lt;BR /&gt;if last.psu and psu=3 then list= '&amp;amp;j='||trim(left(strat));&lt;BR /&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Helvetica',sans-serif; color: #333333;"&gt;*creates macro variable;&lt;BR /&gt;%global list1;&lt;BR /&gt;%let list1=;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Helvetica',sans-serif; color: #333333;"&gt;&lt;BR /&gt;data replist;&lt;BR /&gt;Set stratlist;&lt;BR /&gt;if psu = 3 then&lt;BR /&gt;call symput("list1", trim(resolve('&amp;amp;list1'))||' '||trim(list));&lt;BR /&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Helvetica',sans-serif; color: #333333;"&gt;%put list1=&amp;amp;list1;&amp;nbsp; &amp;nbsp;&amp;nbsp;/* Need to redo this and add in a suffix for year */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Helvetica',sans-serif; color: #333333;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Helvetica',sans-serif; color: #333333;"&gt;I then use this list of strata having 3 PSUs to run&amp;nbsp;the next set of computations for these replicates:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Helvetica',sans-serif; color: #333333;"&gt;%do j = 1 %to 100;&lt;BR /&gt;%if &amp;amp;list1 %then %do;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.5pt; font-family: 'Helvetica',sans-serif; color: #333333;"&gt;%let num = %eval(&amp;amp;num+1);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any useful way to use this?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Nov 2018 15:39:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-programming-macro-to-identify-amp-merge-data-sets/m-p/509572#M137002</guid>
      <dc:creator>SharonZS</dc:creator>
      <dc:date>2018-11-01T15:39:41Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional programming (macro?) to identify &amp; merge data sets?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-programming-macro-to-identify-amp-merge-data-sets/m-p/509574#M137004</link>
      <description>I rarely do this, and not sure I have time for it, but can you email me the full file via DM and I'll take a look? Include one or two variations and I can make some recommendations on how to refactor it for efficiency.</description>
      <pubDate>Thu, 01 Nov 2018 15:42:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-programming-macro-to-identify-amp-merge-data-sets/m-p/509574#M137004</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-01T15:42:34Z</dc:date>
    </item>
  </channel>
</rss>

