<?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 %to in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934796#M367554</link>
    <description>&lt;P&gt;I didn't write all files.&amp;nbsp; FILES include 2008-2022 with same format.&amp;nbsp; Data size very big, so all file data add up together for one file is a good idea or not. I need an efficient way to same variables for each file (different year) to analyze data.&lt;/P&gt;&lt;P&gt;%let styear=2008; %let endyear=2022;&lt;/P&gt;&lt;P&gt;%do tot_year=&amp;amp;styear %to &amp;amp;endyear;&lt;/P&gt;&lt;P&gt;let year =tot_year&lt;/P&gt;&lt;P&gt;proc sql:&lt;/P&gt;&lt;P&gt;create table EX select as&lt;/P&gt;&lt;P&gt;&amp;nbsp;case&lt;BR /&gt;when age ge 10 and age le 14 then pop&amp;amp;year&lt;BR /&gt;else 0&lt;BR /&gt;end as AGES_10_14,&lt;BR /&gt;case&lt;BR /&gt;when age in (15,16,17) then pop&amp;amp;year&lt;BR /&gt;else 0&lt;BR /&gt;end as AGES_15_17&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;from "C:\POP_Data\&amp;amp;year.sas7bdat";&lt;/P&gt;</description>
    <pubDate>Fri, 05 Jul 2024 14:51:56 GMT</pubDate>
    <dc:creator>stella7</dc:creator>
    <dc:date>2024-07-05T14:51:56Z</dc:date>
    <item>
      <title>%do %to</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934789#M367549</link>
      <description>&lt;P&gt;I want to change the following code ,using %do %to, to be more efficient.&lt;/P&gt;&lt;P&gt;If the file name includes "bridge02_08" and then change the name&amp;nbsp; like "b _2008 ",&amp;nbsp; &amp;nbsp; .....................&lt;/P&gt;&lt;P&gt;Removing the redundudnt part.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/**** Read all files in my directory ****************************/&lt;BR /&gt;filename ren pipe 'dir "C:\data\age_years\*"/b' ;&lt;BR /&gt;data ren;&lt;BR /&gt;infile ren;&lt;BR /&gt;input old : $250.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/***** Need to change the file name because I call each file from the same sql code.&lt;/P&gt;&lt;P&gt;data ren_new ;&lt;BR /&gt;set ren;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;old=tranwrd(old, "bridge02_08", "b_2008");&lt;BR /&gt;old=tranwrd(old, "bridge02_09", "b_2009");&lt;BR /&gt;old=tranwrd(old, "bridge02_10", "b_2010");&lt;BR /&gt;old=tranwrd(old, "bridge02_11", "b_2011");&lt;BR /&gt;old=tranwrd(old, "bridge02_12", "b_2012");&lt;BR /&gt;old=tranwrd(old, "bridge02_13", "b_2013");&lt;BR /&gt;old=tranwrd(old, "bridge02_14", "b_2014");&lt;BR /&gt;old=tranwrd(old, "bridge02_15", "b_2015");&lt;BR /&gt;old=tranwrd(old, "bridge02_16", "b_2016");&lt;BR /&gt;old=tranwrd(old, "bridge02_17", "b_2017");&lt;BR /&gt;old=tranwrd(old, "bridge02_18", "b_2018");&lt;BR /&gt;old=tranwrd(old, "bridge02_19", "b_2019");&lt;BR /&gt;old=tranwrd(old, "bridge02_20", "b_2020");&lt;BR /&gt;old=tranwrd(old, "bridge02_21", "b_2021");&lt;BR /&gt;old=tranwrd(old, "bridge02_22", "b_2022");&lt;/P&gt;&lt;P&gt;put old;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jul 2024 13:32:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934789#M367549</guid>
      <dc:creator>stella7</dc:creator>
      <dc:date>2024-07-05T13:32:44Z</dc:date>
    </item>
    <item>
      <title>Re: %do %to</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934791#M367550</link>
      <description>&lt;P&gt;It looks like you might be able to do it with just one statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;old=transtrn(old, "bridge02_", "b_20");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jul 2024 13:44:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934791#M367550</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-07-05T13:44:50Z</dc:date>
    </item>
    <item>
      <title>Re: %do %to</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934793#M367552</link>
      <description>&lt;P&gt;I need to put up year for each bridge01_08a&amp;nbsp; &amp;nbsp;for year 2008&amp;nbsp; ,&amp;nbsp; bridge01_09a for y2009..... These are&amp;nbsp; file names, so I call&amp;nbsp; each file name inside proc sql code.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%let startyr = 2008;&lt;BR /&gt;%let endyr = 2022;&lt;/P&gt;&lt;P&gt;%do tot_year=2008 %to 2022;&lt;/P&gt;&lt;P&gt;%let year=tot_year;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table pop as select *&lt;/P&gt;&lt;P&gt;case when.....&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;from "C:\POP_Data\bridge01_08a.sas7bdat"; ====&amp;gt; C:\POP_Data\b&amp;amp;year.sas7bdat"&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jul 2024 14:53:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934793#M367552</guid>
      <dc:creator>stella7</dc:creator>
      <dc:date>2024-07-05T14:53:02Z</dc:date>
    </item>
    <item>
      <title>Re: %do %to</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934795#M367553</link>
      <description>&lt;P&gt;Sorry, I'm not understanding what you are asking.&amp;nbsp; Can you describe more of the big picture?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It looks like you have a directory full of SAS datasets named like :&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;bridge01_08a.sas7bdat&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;bridge01_09a.sas7bdat&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;bridge01_10a.sas7bdat&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Is that right?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;And what do you want to do with those datasets?&amp;nbsp; Do you want to rename the datasets?&amp;nbsp; Or are you trying to analyze the data in them?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Do all of these datasets have the same variables in them, so that it would be feasible to stack them into one dataset, with a variable for year?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;If you forget about macro code, can you show the full code that would do what you want, for say just 3 datasets?&amp;nbsp; Once that is clear, we can help you think about how to automate it with macro code, or some other approach.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jul 2024 14:35:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934795#M367553</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-07-05T14:35:59Z</dc:date>
    </item>
    <item>
      <title>Re: %do %to</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934796#M367554</link>
      <description>&lt;P&gt;I didn't write all files.&amp;nbsp; FILES include 2008-2022 with same format.&amp;nbsp; Data size very big, so all file data add up together for one file is a good idea or not. I need an efficient way to same variables for each file (different year) to analyze data.&lt;/P&gt;&lt;P&gt;%let styear=2008; %let endyear=2022;&lt;/P&gt;&lt;P&gt;%do tot_year=&amp;amp;styear %to &amp;amp;endyear;&lt;/P&gt;&lt;P&gt;let year =tot_year&lt;/P&gt;&lt;P&gt;proc sql:&lt;/P&gt;&lt;P&gt;create table EX select as&lt;/P&gt;&lt;P&gt;&amp;nbsp;case&lt;BR /&gt;when age ge 10 and age le 14 then pop&amp;amp;year&lt;BR /&gt;else 0&lt;BR /&gt;end as AGES_10_14,&lt;BR /&gt;case&lt;BR /&gt;when age in (15,16,17) then pop&amp;amp;year&lt;BR /&gt;else 0&lt;BR /&gt;end as AGES_15_17&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;from "C:\POP_Data\&amp;amp;year.sas7bdat";&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jul 2024 14:51:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934796#M367554</guid>
      <dc:creator>stella7</dc:creator>
      <dc:date>2024-07-05T14:51:56Z</dc:date>
    </item>
    <item>
      <title>Re: %do %to</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934798#M367556</link>
      <description>&lt;P&gt;SAS is very good at analyzing big data.&amp;nbsp; If you keep the data in different files for each year, you will constantly need to analyze multiple files.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would try putting them together into one big file.&amp;nbsp; When you do this, you can keep only the variables you need, and only the rows you need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to combine all SAS datasets in a folder that start with the letter B into one dataset, you could do it like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname bridge 'C:\POP_Data' ;
data bridges ;
  set bridge.b: indsname=_dsname ;
  length dsname $32 ;
  dsname=scan(_dsname,2,'.') ;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The variable DSNAME will show then name of the file which was the source for each record.&amp;nbsp; And you can use that to make a variable for year, or whatever logic.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jul 2024 15:05:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934798#M367556</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-07-05T15:05:39Z</dc:date>
    </item>
    <item>
      <title>Re: %do %to</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934814#M367557</link>
      <description>&lt;P&gt;You need to explain the bigger picture better to allow us to suggest ways to get what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is very hard to get SAS macro code to generate the SAS code you want when you don't yet know what SAS code you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example the snippet you just showed does not make much sense because you have a %DO loop that is regenerating the exact same dataset over and over.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do tot_year=&amp;amp;styear %to &amp;amp;endyear;
...
  create table EX  ...
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;At the end of the %DO loop that dataset EX will be what was made when TOT_YEAR had the value of ENDYEAR.&amp;nbsp; The dataset created for the other years will have been replaced.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jul 2024 15:44:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934814#M367557</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-07-05T15:44:41Z</dc:date>
    </item>
    <item>
      <title>Re: %do %to</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934815#M367558</link>
      <description>&lt;P&gt;It is not clear what you want to do here, but let's show some simple macro coding methods that might help with generating those types of strings from a YEAR value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The main tricky thing is that the year part in your strings do not include the century, so you have strings with leading zeros.&amp;nbsp; One way to make sure one digit numbers get generated with leading zeros is to use the %SYSFUNC() to call the PUTN() function and apply the Z format to the number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do year = 8 to 22 ;
   %let twodigit = %sysfunc(putn(&amp;amp;year,z2.));
   %let oldname=bridge02_&amp;amp;twodigit;
   %let newname=b_20&amp;amp;twodigit;
....
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Another way is to use the real year values (with the century).&amp;nbsp; Then you could use the %SUBSTR() macro function to pick off the last 2 digits.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do year = 2008 to 2022 ;
   %let twodigit = %substr(&amp;amp;year,3);
   %let oldname=bridge02_&amp;amp;twodigit;
   %let newname=b_20&amp;amp;twodigit;
....
%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 Jul 2024 16:02:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934815#M367558</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-07-05T16:02:13Z</dc:date>
    </item>
    <item>
      <title>Re: %do %to</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934854#M367573</link>
      <description>&lt;P&gt;Thanks . The code is simple and very easy to add up all dataset.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jul 2024 21:34:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934854#M367573</guid>
      <dc:creator>stella7</dc:creator>
      <dc:date>2024-07-05T21:34:59Z</dc:date>
    </item>
    <item>
      <title>Re: %do %to</title>
      <link>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934856#M367574</link>
      <description>&lt;P&gt;I tried to use this code and change values in column , "old".&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="stella7_0-1720215519992.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/98198iF35AD913E883F5AF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="stella7_0-1720215519992.png" alt="stella7_0-1720215519992.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This data file name. I want to change to b_2008.sas7bdat, b_2009.sas7bdat, b_2010.sas7bdat............b_2022.sas7bdat.&lt;/P&gt;&lt;P&gt;In your code, I put&amp;nbsp;&amp;nbsp;:&amp;nbsp; &amp;nbsp; old=%sysfunc(tranwrd(old, &amp;amp;oldname, &amp;amp;newname)); However, it doesn't work.&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;%do year = 2008 to 2022 ;
   %let twodigit = %substr(&amp;amp;year,3);
   %let oldname=bridge02_&amp;amp;twodigit;
   %let newname=b_20&amp;amp;twodigit;
    old=%sysfunc(tranwrd(old, &amp;amp;oldname, &amp;amp;newname));
%end;&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 05 Jul 2024 21:44:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/do-to/m-p/934856#M367574</guid>
      <dc:creator>stella7</dc:creator>
      <dc:date>2024-07-05T21:44:13Z</dc:date>
    </item>
  </channel>
</rss>

