<?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: macro do loop with inconsecutive number in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423737#M104244</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/5700"&gt;@owenwqp1&lt;/a&gt; wrote:&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; Thank you Tom. The key point of my problem is that, for some states and even the same state,the same variables in different years may be of different type because in some years variable X1 may be numeric and in another year X1 becomes a character variable when imported to sas due to lack of data in that year. So when I use wild cards, it does not work.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So finally you have stated an actual problem.&amp;nbsp; This is caused by using PROC IMPORT to guess about what is in your source files.&amp;nbsp; If you can convert the source files to text files (like CSV files for example) then you can read them in consistently and you will no longer have this problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your problem is just with empty variables that got created as numeric instead of character then the solution is to just drop them. (if they are empty you will lose nothing).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Automating this might be more work than its worth.&amp;nbsp; Personally I would just get the list of files.&amp;nbsp; For example you could run a simple step like this&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set sashelp.vtable ;
  where libname = 'WORK' and memname like 'FLORIDA_%' ;
  put memname '(drop=)';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and then&amp;nbsp; copy the list from the log into a new data step and add in any variables that need to be dropped.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data all_florida ;
  set
FLORIDA_101 (drop=)
FLORIDA_102 (drop=X1)
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really need to automate then you should create a dataset to use as a template and query the metadata for your new files and compare the the expected result.&amp;nbsp; Then you can use the result of that comparison to generate appropriate code for each dataset that will drop ,&amp;nbsp;rename , transform etc any variables that got defined in the wrong way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As to the question in your title the example data step above is probably a good place to start for the "looping".&amp;nbsp; So instead of looping in macro code make the normal data step iteration your loop.&amp;nbsp; For example if you created a macro named %FIX() that&amp;nbsp; could fix up the variables for one of the files then you could use CALL EXECUTE in a data step to call it for all of the datasets.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set sashelp.vtable ;
  where libname='WORK' and memname like 'FLORIDA_%' ;
  call execute(cats('%nrstr(%fix)(',memname,')'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 27 Dec 2017 15:29:53 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-12-27T15:29:53Z</dc:date>
    <item>
      <title>macro do loop with inconsecutive number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423615#M104194</link>
      <description>Hi everyone,

I am dealing with 3 digit sic industry data by states. My data in excel are with names like Florida_101, Texas_469 and so on. When I import  with macro do loop like "%do i=101 %to 469", it works although  for each state the 3 digit sic industry are not consecutive from 101 to 469, because  physical excel datasets for those non-sic 3 digit industry code  numbers are missing. But when I use data step to deal with variables in the imported datasets(with do loop in macro: %do i=101 %to 469), I got many empty datasets.  How could I solve this problem?  macro like %data(i,j) are not acceptable because there will be a very long list of %data(i,j).

Thanks!</description>
      <pubDate>Tue, 26 Dec 2017 17:49:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423615#M104194</guid>
      <dc:creator>owenwqp1</dc:creator>
      <dc:date>2017-12-26T17:49:42Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop with inconsecutive number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423623#M104196</link>
      <description>&lt;P&gt;Take a look at the EXIST and FEXIST functions.&amp;nbsp; They tell you whether a SAS data set exists (EXIST) or a file exists (FEXIST).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Inside your %DO loop,&amp;nbsp; you can add an interior %DO group that basically says, "If the file I'm looking for exists, then %DO."&lt;/P&gt;</description>
      <pubDate>Tue, 26 Dec 2017 19:36:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423623#M104196</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-12-26T19:36:00Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop with inconsecutive number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423647#M104209</link>
      <description>&lt;P&gt;You need to provide more information if you want help. Why would a loop generating data step code be any different than a loop generating proc import code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are you looping from a to b if every number in between a and b does not exist?&amp;nbsp; Why not set it up to loop over a list of valid values?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let valid_list=Florida_101 Texas_469 ;
%do i=1 %to %sysfunc(coutw(&amp;amp;valid_list,%str ));
  %let next=%scan(&amp;amp;valid_list,&amp;amp;i,%str( ));
  ....
%end;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Dec 2017 22:24:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423647#M104209</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-12-26T22:24:55Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop with inconsecutive number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423684#M104223</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;
Thank you Tom.
To be more concrete, I have sic3 data of states. For example, I have Florida_101, Florida_102, Florida_103, Florida_105, Florida_201, Florida_202, Florida_203, Florida_204, Florida_209, Florida_301, Florida_302, Florida_303,Florida_304, Florida_306; 

and for Texas, I also have Texas_101, Texas_102, Texas_103, Texas_105, Texas_201, Texas_202, Texas_203, Texas_204, Texas_209, Texas_301, Texas_302, Texas_303,Texas_304, Texas_306.
These original data are in excel format, I imported into sas with "%do i=101 %to306". I got only Florida_101, Florida_102, Florida_103, Florida_105, Florida_201, Florida_202, Florida_203, Florida_204, Florida_209, Florida_301, Florida_302, Florida_303,Florida_304, Florida_306 and Texas_101, Texas_102, Texas_103, Texas_105, Texas_201, Texas_202, Texas_203, Texas_204, Texas_209, Texas_301, Texas_302, Texas_303,Texas_304, Texas_306 in sas format.
But when I use data step, I got Florida_101.......... to ..........Florida_306, more than 200 datasets in sas. I want to know how to get only Florida_101, Florida_102, Florida_103, Florida_105, Florida_201, Florida_202, Florida_203, Florida_204, Florida_209, Florida_301, Florida_302, Florida_303,Florida_304, Florida_306 when I use data step.Thanks!</description>
      <pubDate>Wed, 27 Dec 2017 07:48:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423684#M104223</guid>
      <dc:creator>owenwqp1</dc:creator>
      <dc:date>2017-12-27T07:48:03Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop with inconsecutive number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423689#M104225</link>
      <description>&lt;P&gt;Store your valid value comnibations in a dataset, and use call execute off that dataset. Much better and scalable than a list in a macro variable.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Dec 2017 08:49:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423689#M104225</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-12-27T08:49:01Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop with inconsecutive number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423722#M104238</link>
      <description>&lt;P&gt;Still not sure if I understand.&amp;nbsp; If you imported them into WORK library and you want all of the FLORIDA datasets then why not just use wild cards.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data all_florida ;
  set florida_: ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Dec 2017 13:54:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423722#M104238</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-12-27T13:54:03Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop with inconsecutive number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423728#M104240</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;

Thank you KurtBremser! But I have no idea about the call execute routine,could you kindly give me the sample code about my problem so that  I can start with?  Thanks a lot!</description>
      <pubDate>Wed, 27 Dec 2017 14:21:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423728#M104240</guid>
      <dc:creator>owenwqp1</dc:creator>
      <dc:date>2017-12-27T14:21:41Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop with inconsecutive number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423731#M104242</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;
Thank you Tom. The key point of my problem is that, for some states and even the same state,the same variables in different years may be of different type because in some years variable X1 may be numeric and in another year X1 becomes a character variable when imported to sas due to lack of data in that year. So when I use wild cards, it does not work.</description>
      <pubDate>Wed, 27 Dec 2017 14:30:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423731#M104242</guid>
      <dc:creator>owenwqp1</dc:creator>
      <dc:date>2017-12-27T14:30:45Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop with inconsecutive number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423737#M104244</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/5700"&gt;@owenwqp1&lt;/a&gt; wrote:&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; Thank you Tom. The key point of my problem is that, for some states and even the same state,the same variables in different years may be of different type because in some years variable X1 may be numeric and in another year X1 becomes a character variable when imported to sas due to lack of data in that year. So when I use wild cards, it does not work.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So finally you have stated an actual problem.&amp;nbsp; This is caused by using PROC IMPORT to guess about what is in your source files.&amp;nbsp; If you can convert the source files to text files (like CSV files for example) then you can read them in consistently and you will no longer have this problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your problem is just with empty variables that got created as numeric instead of character then the solution is to just drop them. (if they are empty you will lose nothing).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Automating this might be more work than its worth.&amp;nbsp; Personally I would just get the list of files.&amp;nbsp; For example you could run a simple step like this&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set sashelp.vtable ;
  where libname = 'WORK' and memname like 'FLORIDA_%' ;
  put memname '(drop=)';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and then&amp;nbsp; copy the list from the log into a new data step and add in any variables that need to be dropped.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data all_florida ;
  set
FLORIDA_101 (drop=)
FLORIDA_102 (drop=X1)
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really need to automate then you should create a dataset to use as a template and query the metadata for your new files and compare the the expected result.&amp;nbsp; Then you can use the result of that comparison to generate appropriate code for each dataset that will drop ,&amp;nbsp;rename , transform etc any variables that got defined in the wrong way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As to the question in your title the example data step above is probably a good place to start for the "looping".&amp;nbsp; So instead of looping in macro code make the normal data step iteration your loop.&amp;nbsp; For example if you created a macro named %FIX() that&amp;nbsp; could fix up the variables for one of the files then you could use CALL EXECUTE in a data step to call it for all of the datasets.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set sashelp.vtable ;
  where libname='WORK' and memname like 'FLORIDA_%' ;
  call execute(cats('%nrstr(%fix)(',memname,')'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Dec 2017 15:29:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423737#M104244</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-12-27T15:29:53Z</dc:date>
    </item>
    <item>
      <title>Re: macro do loop with inconsecutive number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423782#M104258</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/5700"&gt;@owenwqp1&lt;/a&gt; wrote:&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; Thank you KurtBremser! But I have no idea about the call execute routine,could you kindly give me the sample code about my problem so that I can start with? Thanks a lot!&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I see that &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;has already given you a nice example in the meantime.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Dec 2017 19:46:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-do-loop-with-inconsecutive-number/m-p/423782#M104258</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-12-27T19:46:45Z</dc:date>
    </item>
  </channel>
</rss>

