<?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 in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/560802#M10501</link>
    <description>&lt;P&gt;You can nest macro %DO loops, similar to how you can nest data step DO loops.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below example I added two parameters to the macro, which allow you to input the first year and the last year.&amp;nbsp; The macro then loops over the years, and within each year, loops over the week numbers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro sqlloop
  (yearmin=
  ,yearmax=
  )
;
%local n year ;

%do year=&amp;amp;yearmin %to &amp;amp;yearmax ;
  %do n=2 %to 53;
    %put &amp;amp;n &amp;amp;year wk&amp;amp;n._&amp;amp;year ;
  %end;
%end ;

%mend;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;%sqlloop(yearmin=2017,yearmax=2019)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that wk&amp;amp;n._&amp;amp;year resolves to wk2_17.&amp;nbsp; The dot after &amp;amp;n is needed to end the macro variable reference.&amp;nbsp; Otherwise the macro processor would look for a macro variable named n_.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 22 May 2019 12:36:21 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2019-05-22T12:36:21Z</dc:date>
    <item>
      <title>MACRO DO LOOP</title>
      <link>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/560766#M10492</link>
      <description>&lt;P&gt;Hi ,&lt;/P&gt;&lt;P&gt;I met with problem trying to add new columns for week from 2013 onwards. Eg. wk1 -2013....wk25-2019. Although i was able to obtain do loop for wk1 to wk53 dont know how to add wk1-2013 ..etc&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro sqlloop;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table SMGCAMPAIGNS_week_Add as&lt;BR /&gt;select *,&lt;BR /&gt;case when myweek = 1 a then 1 else 0 end as wk1&lt;BR /&gt;%do n=2 %to 53;&lt;BR /&gt;,case when myweek = &amp;amp;n&amp;nbsp; then 1 else 0 end as wk&amp;amp;n&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;from SMGCAMPAIGNS_final;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;%mend;&lt;BR /&gt;%sqlloop;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there way to add loop years from 2013 onwards and create columns accordingly&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 May 2019 11:22:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/560766#M10492</guid>
      <dc:creator>meckarthik</dc:creator>
      <dc:date>2019-05-22T11:22:12Z</dc:date>
    </item>
    <item>
      <title>Re: MACRO DO LOOP</title>
      <link>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/560785#M10494</link>
      <description>&lt;P&gt;You try to transpose your data. SAS provides proc transpose for this.&lt;/P&gt;
&lt;P&gt;If you provide some example for the dataset&amp;nbsp;&lt;SPAN&gt;SMGCAMPAIGNS_final, I can give you an example for the usage of transpose.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 22 May 2019 11:36:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/560785#M10494</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-05-22T11:36:27Z</dc:date>
    </item>
    <item>
      <title>Re: MACRO DO LOOP</title>
      <link>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/560791#M10496</link>
      <description>What is your ultimate plan here?  If you provide a few details, there may be a way to get there without the need for macro language or hundreds of new variables.</description>
      <pubDate>Wed, 22 May 2019 12:02:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/560791#M10496</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-05-22T12:02:28Z</dc:date>
    </item>
    <item>
      <title>Re: MACRO DO LOOP</title>
      <link>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/560792#M10497</link>
      <description>&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Weeks&amp;nbsp; &amp;nbsp; Years&lt;/P&gt;&lt;P&gt;681840&amp;nbsp; &amp;nbsp;22&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2019&lt;BR /&gt;682346&amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2019&lt;BR /&gt;682404&amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2019&lt;BR /&gt;677570&amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2019&lt;BR /&gt;677573&amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2019&lt;BR /&gt;681545&amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2019&lt;BR /&gt;681667&amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2019&lt;BR /&gt;681542&amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; 2019&lt;/P&gt;</description>
      <pubDate>Wed, 22 May 2019 12:07:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/560792#M10497</guid>
      <dc:creator>meckarthik</dc:creator>
      <dc:date>2019-05-22T12:07:11Z</dc:date>
    </item>
    <item>
      <title>Re: MACRO DO LOOP</title>
      <link>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/560796#M10499</link>
      <description>&lt;P&gt;want to create new columns based on weeks and year listed in my table.&amp;nbsp; if the weeks or year not exist then dont create the columns. For example week 1 - 2013 may be in data then create week1-2013 else don't. Assume got 5 years of dataset, not all months /weeks got the data.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Weeks Year&lt;/P&gt;&lt;P&gt;681840&amp;nbsp; &amp;nbsp; 22&amp;nbsp; &amp;nbsp;2019&lt;BR /&gt;682346&amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp;2019&lt;BR /&gt;682404&amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp;2019&lt;BR /&gt;677570&amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp;2019&lt;BR /&gt;677573&amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp;2019&lt;BR /&gt;681545&amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp;2019&lt;BR /&gt;681667&amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp;2019&lt;BR /&gt;681542&amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp;2019&lt;/P&gt;</description>
      <pubDate>Wed, 22 May 2019 12:10:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/560796#M10499</guid>
      <dc:creator>meckarthik</dc:creator>
      <dc:date>2019-05-22T12:10:34Z</dc:date>
    </item>
    <item>
      <title>Re: MACRO DO LOOP</title>
      <link>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/560797#M10500</link>
      <description>&lt;P&gt;See this code and play around with it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $ Weeks Years;
datalines;
681840  22 2019
682346  20 2019
682404  20 2019
677570  20 2019
677573  20 2019
681545  20 2019
681667  20 2019
681542  20 2019
;

data pretrans;
set have;
/* build the future column name */
idvar = 'period_' !! put(years,z4.) !! put(weeks,z2.);
/* set the value we later want to have */
dummy = 1;
output;
/* create a dummy template for all expected periods */
if _n_ = 1
then do;
  id = ' '; /* so it will be first after the sort */
  do years = 2013 to 2019;
    do weeks = 0 to 53;
      idvar = 'period_' !! put(years,z4.) !! put(weeks,z2.);
      output;
    end;
  end;
end;
run;

proc sort data=pretrans;
by id;
run;

proc transpose
  data=pretrans
  out=want (
    drop=_name_
    where=(id &amp;gt; " ")
    /* filters the dummy table */
  )
;
by id;
id idvar;
var dummy;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 May 2019 12:24:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/560797#M10500</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-05-22T12:24:10Z</dc:date>
    </item>
    <item>
      <title>Re: MACRO DO LOOP</title>
      <link>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/560802#M10501</link>
      <description>&lt;P&gt;You can nest macro %DO loops, similar to how you can nest data step DO loops.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below example I added two parameters to the macro, which allow you to input the first year and the last year.&amp;nbsp; The macro then loops over the years, and within each year, loops over the week numbers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro sqlloop
  (yearmin=
  ,yearmax=
  )
;
%local n year ;

%do year=&amp;amp;yearmin %to &amp;amp;yearmax ;
  %do n=2 %to 53;
    %put &amp;amp;n &amp;amp;year wk&amp;amp;n._&amp;amp;year ;
  %end;
%end ;

%mend;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;%sqlloop(yearmin=2017,yearmax=2019)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that wk&amp;amp;n._&amp;amp;year resolves to wk2_17.&amp;nbsp; The dot after &amp;amp;n is needed to end the macro variable reference.&amp;nbsp; Otherwise the macro processor would look for a macro variable named n_.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 May 2019 12:36:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/560802#M10501</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2019-05-22T12:36:21Z</dc:date>
    </item>
    <item>
      <title>Re: MACRO DO LOOP</title>
      <link>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/560864#M10516</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/275095"&gt;@meckarthik&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;want to create new columns based on weeks and year listed in my table.&amp;nbsp; if the weeks or year not exist then dont create the columns. For example week 1 - 2013 may be in data then create week1-2013 else don't. Assume got 5 years of dataset, not all months /weeks got the data.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Weeks Year&lt;/P&gt;
&lt;P&gt;681840&amp;nbsp; &amp;nbsp; 22&amp;nbsp; &amp;nbsp;2019&lt;BR /&gt;682346&amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp;2019&lt;BR /&gt;682404&amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp;2019&lt;BR /&gt;677570&amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp;2019&lt;BR /&gt;677573&amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp;2019&lt;BR /&gt;681545&amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp;2019&lt;BR /&gt;681667&amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp;2019&lt;BR /&gt;681542&amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp;2019&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;What is the purpose for all of these columns? Do you actually need a data set or a report that people read? Data with changing numbers of columns is quite often a problem to manipulate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does your data already contain a variable with SAS date values?&lt;/P&gt;</description>
      <pubDate>Wed, 22 May 2019 14:51:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/560864#M10516</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-05-22T14:51:56Z</dc:date>
    </item>
    <item>
      <title>Re: MACRO DO LOOP</title>
      <link>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/561071#M10531</link>
      <description>&lt;P&gt;it works , thank you&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2019 08:26:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/561071#M10531</guid>
      <dc:creator>meckarthik</dc:creator>
      <dc:date>2019-05-23T08:26:49Z</dc:date>
    </item>
    <item>
      <title>Re: MACRO DO LOOP</title>
      <link>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/561072#M10532</link>
      <description>&lt;P&gt;it&amp;nbsp; works, thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2019 08:27:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/MACRO-DO-LOOP/m-p/561072#M10532</guid>
      <dc:creator>meckarthik</dc:creator>
      <dc:date>2019-05-23T08:27:18Z</dc:date>
    </item>
  </channel>
</rss>

