<?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: Do loop before data statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324535#M271320</link>
    <description>&lt;P&gt;So if I understand correctly, I have to open up each individual dataset and create a work.xxx dataset out of them.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If that's the case, I might as well do the entire thing with copy/paste and run the code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But thanks for the response.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 13 Jan 2017 12:07:28 GMT</pubDate>
    <dc:creator>GKati</dc:creator>
    <dc:date>2017-01-13T12:07:28Z</dc:date>
    <item>
      <title>Do loop before data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324524#M271314</link>
      <description>&lt;P&gt;Hello SAS users,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have 36 individual datasets (one dataset per month for 3 years) titled "monthyear" (eg. "12012" for Jan 2012). I want to open then up, sort them, create a new variable and close them up. Preferebly, in an automated fashion.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In STATA, I would be able to write a loop for it. I would start the loop BEFORE opening up the dataset, and have it run through values i=1/12, j=1/3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In SAS this would look something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;do i= 1 to 12;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;do j= 1/3;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;data new_'i'201'j';&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;set&amp;nbsp;&lt;SPAN&gt;'i'201'j'&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;sort.....;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;newvar...;&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, in SAS the DO statement must be preceded by the DATA statement that specifies the dataset. Therefore, this won't run.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a solution for this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2017 11:39:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324524#M271314</guid>
      <dc:creator>GKati</dc:creator>
      <dc:date>2017-01-13T11:39:02Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop before data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324526#M271315</link>
      <description>&lt;P&gt;Well, to start with 12012 is not a valid name for a dataset, so your datasets aren't called that. &amp;nbsp;Also, is the year always 4 digits? &amp;nbsp;It would be a good idea to have some sort of standardisation to make finding month easier, is it 1 digit or 2? &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now as for looping, thats pretty simple, you have the metadata information in sashelp.vtable (and vcolumns for columns) and from that you can simply create your code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data _null_;
  set sashelp.vtable (where=(libname="&amp;lt;yourlib&amp;gt;" and substr(memname,1,4)="DTA1"));
  call execute('data '||catx('.',libname,memname)||'; set '||catx('.',libname,memname)||'; do some code here; run;');
run;&lt;/PRE&gt;
&lt;P&gt;What this does is open the SAS metadata, and take only rows with given libname and prefix of DTA1 (as your given names are incorrect). &amp;nbsp;Then for each of these datasets it will generate a datastep with the code.&lt;/P&gt;
&lt;P&gt;Here is an example:&lt;/P&gt;
&lt;PRE&gt;data work.dta1_012012;
  set sashelp.class;
run;
data work.dta1_022012;
  set sashelp.class;
run;
data _null_;
  set sashelp.vtable (where=(libname="WORK" and substr(memname,1,4)="DTA1"));
  call execute('data '||catx('.',libname,memname)||'; set '||catx('.',libname,memname)||'; myflag=1; run;');
run;
&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Jan 2017 11:46:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324526#M271315</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-01-13T11:46:53Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop before data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324528#M271316</link>
      <description>&lt;P&gt;Are you sure on the existing data set names like 12012, 21012 ..and so on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data set name should start with either '_' or a 'character' . Clarify on the existing source data.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2017 11:58:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324528#M271316</guid>
      <dc:creator>Vish33</dc:creator>
      <dc:date>2017-01-13T11:58:15Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop before data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324530#M271317</link>
      <description>&lt;P&gt;Yes, the name is as described. I had trouble with it but with '' before/after the name helped.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the responses so far.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2017 12:01:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324530#M271317</guid>
      <dc:creator>GKati</dc:creator>
      <dc:date>2017-01-13T12:01:03Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop before data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324531#M271318</link>
      <description>&lt;P&gt;And while we're at defining names for datasets, the sequence in the filename should be year - month instead of month - year. Much easier for handling in the future.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2017 12:01:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324531#M271318</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-01-13T12:01:57Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop before data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324533#M271319</link>
      <description>&lt;P&gt;Thanks, will remember for future. For now, the name was pre-defined.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2017 12:05:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324533#M271319</guid>
      <dc:creator>GKati</dc:creator>
      <dc:date>2017-01-13T12:05:15Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop before data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324535#M271320</link>
      <description>&lt;P&gt;So if I understand correctly, I have to open up each individual dataset and create a work.xxx dataset out of them.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If that's the case, I might as well do the entire thing with copy/paste and run the code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But thanks for the response.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2017 12:07:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324535#M271320</guid>
      <dc:creator>GKati</dc:creator>
      <dc:date>2017-01-13T12:07:28Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop before data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324537#M271321</link>
      <description>&lt;P&gt;No, this code will generate the necessary code to perform the same datastep on every dataset name returned by the where clause. &amp;nbsp;Try running the code and you will see what I mean. &amp;nbsp;You do not have to copy/paste or anything else, just run that code.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2017 12:18:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324537#M271321</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-01-13T12:18:47Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop before data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324538#M271322</link>
      <description>&lt;P&gt;The other solution is to write a macro, so %do loop.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.ats.ucla.edu/stat/sas/seminars/sas_macros_introduction/" target="_blank"&gt;http://www.ats.ucla.edu/stat/sas/seminars/sas_macros_introduction/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2017 12:23:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324538#M271322</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-01-13T12:23:40Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop before data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324541#M271323</link>
      <description>&lt;P&gt;Try this way ..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;　&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;%Macro&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; test(libname=,prefix=,newvar=);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;/**listing the data from a particular library where the actual data resides and by giving proper condition**/&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;create table listData as&lt;/P&gt;
&lt;P&gt;select memname&lt;/P&gt;
&lt;P&gt;from dictionary.tables&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;where libname=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"&amp;amp;libname"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; and memname like &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"&amp;amp;prefix%"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; ;&lt;/FONT&gt;&lt;FONT color="#008000" face="Courier New" size="3"&gt;/*here prefix or suffix how you are identifying data set*/&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;/**Count of total no.of data sets **/&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;select count(*) into :cnt from listData;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;/**create macro variables for each data set..lets say in your case it is 36**/&lt;/P&gt;
&lt;P&gt;/**like memname1=12012 memname2=22012......memname36=122014**/&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%do&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; i=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt; to &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;cnt.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;select memname into :memname&amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;i.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;-memname&amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;cnt.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; from listData;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%end&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;/** calling one by one using do loop and create the required new varibles**/&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; &lt;STRONG&gt;&lt;I&gt;all&lt;/I&gt;&lt;/STRONG&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%do&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; i=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt; to &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;cnt.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; memname=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%sysfunc&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(compress(&amp;amp;&amp;amp;memname&amp;amp;i.)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;data new_&amp;amp;memname.;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt; set &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;memname.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#008000" face="Courier New" size="3"&gt;/*sort */&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt; &amp;amp;newvar=;&lt;/FONT&gt;&lt;FONT color="#008000" face="Courier New" size="3"&gt;/*apply logic here if this is common for all*/&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%end&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;%&lt;STRONG&gt;&lt;I&gt;all&lt;/I&gt;&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;/*invoke the macro by giving your library name and prefix or suffix*/&lt;/P&gt;
&lt;P&gt;%&lt;STRONG&gt;&lt;I&gt;test&lt;/I&gt;&lt;/STRONG&gt;(libname=,prefix=,newvar=);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2017 13:08:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324541#M271323</guid>
      <dc:creator>Vish33</dc:creator>
      <dc:date>2017-01-13T13:08:02Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop before data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324561#M271324</link>
      <description>&lt;P&gt;Hi.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a macro I believe will do the cycle for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro doit(PREFIX,NAME,SUFIX);

* enable extended data names support;
options VALIDMEMNAME=EXTEND; 

%do _P=1 %to PREFIX;
%do _S=1 %to SUFIX;

* create new var;
data "&amp;amp;_P&amp;amp;NAME&amp;amp;_S"n;
        set "&amp;amp;_P&amp;amp;NAME&amp;amp;_S"n;
        &amp;lt;new_var&amp;gt;=&amp;lt;some_value&amp;gt;
run;

* sort the data by desired var;
proc sort;
        by &amp;lt;desired_var&amp;gt;;
run;

%end;
%end;
%mend doit;

%doit(12,201,3); * do it;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As said previously, data set naming explicitly forbids the use of a number at first position.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For that you must enable extended name support with the VALIDMEMNAME option. Then you can name you data whatever you want if enclosed in '&amp;lt;data name&amp;gt;'n or "&amp;lt;data name&amp;gt;"n.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;More on names in SAS here:&amp;nbsp;&lt;A href="http://support.sas.com/documentation/cdl/en/lrcon/69852/HTML/default/viewer.htm#p18cdcs4v5wd2dn1q0x296d3qek6.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrcon/69852/HTML/default/viewer.htm#p18cdcs4v5wd2dn1q0x296d3qek6.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Hope it helps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Daniel Santos&amp;nbsp;@ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2017 14:08:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-before-data-statement/m-p/324561#M271324</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2017-01-13T14:08:25Z</dc:date>
    </item>
  </channel>
</rss>

