<?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: Help Needed ASAP -  Writing Do loop with dynamic variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-Needed-ASAP-Writing-Do-loop-with-dynamic-variables/m-p/267979#M53004</link>
    <description>&lt;P&gt;Please find full code &amp;nbsp;and input and output data attached.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Libname worklib '/mnt/silverbhw/Common_Share/Temp/IDM/Work/';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data worklib.ExposureC;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; set worklib.ExposureB;&lt;/P&gt;&lt;P&gt;/* Exposure months*/&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;format mmdate ddmmyy10.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Do i = 2010 to 2010;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Do j = 1 to 2;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;k = 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;mmdate=mdy(j,k,i);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;output;&lt;BR /&gt;&lt;BR /&gt;start_date = FROM_DATE;&lt;BR /&gt;enddate = TO_DATE;&lt;BR /&gt;eom=intnx( 'month', mmdate,0,'end');&lt;BR /&gt;numdays=day(eom);&lt;BR /&gt;output;&lt;/P&gt;&lt;P&gt;x = 0;&lt;/P&gt;&lt;P&gt;if mmdate&amp;gt;= start_date and mmdate&amp;lt;= enddate then&lt;BR /&gt;x = numdays;&lt;/P&gt;&lt;P&gt;if year(mmdate)= year(start_date) and month(mmdate)= month(start_date) then&lt;BR /&gt;x = numdays - day(start_date);&lt;/P&gt;&lt;P&gt;if year(mmdate)= year(enddate) and month(mmdate)= month(enddate) then&lt;BR /&gt;x = day(enddate);&lt;BR /&gt;Varname = x;&lt;BR /&gt;output;&lt;BR /&gt;&lt;BR /&gt;End;&lt;BR /&gt;End;&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Tue, 03 May 2016 15:10:05 GMT</pubDate>
    <dc:creator>Kuda35</dc:creator>
    <dc:date>2016-05-03T15:10:05Z</dc:date>
    <item>
      <title>Help Needed ASAP -  Writing Do loop with dynamic variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-Needed-ASAP-Writing-Do-loop-with-dynamic-variables/m-p/267751#M52949</link>
      <description>&lt;P&gt;I am trying to run a nested &amp;nbsp;do loop &amp;nbsp;that creates &amp;nbsp;columns "EXP_&amp;amp;i&amp;amp;j" and sets the appropriate values. Currently using the following code below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately not seeing any values coming through for i,j,mmdate,eom,numdays,x. &amp;nbsp;Not sure what the issue is. Please help&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%Do i = 2010 %to 2010;&lt;BR /&gt;%Do j = 1 %to 2;&lt;BR /&gt;k = 1;&lt;BR /&gt;&lt;BR /&gt;mmdate=mdy(j,k,i);&lt;BR /&gt;format mmdate ddmmyy10.;&lt;/P&gt;&lt;P&gt;enddate = TO_DATE;&lt;BR /&gt;eom=intnx( 'month', mmdate,0,'end');&lt;BR /&gt;numdays=day(eom);&lt;/P&gt;&lt;P&gt;x = 0;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;if mmdate&amp;gt;= start_date and mmdate&amp;lt;= enddate then x = numdays;&lt;BR /&gt;if year(mmdate)= year(start_date) and month(mmdate)= month(start_date) then x = numdays - day(start_date);&lt;BR /&gt;if year(mmdate)= year(enddate) and month(mmdate)= month(enddate) then x = day(enddate);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;EXP_&amp;amp;i&amp;amp;j = x;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;output;&lt;BR /&gt;%End;&lt;BR /&gt;%End;&lt;BR /&gt;%mend A;&lt;BR /&gt;%A;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 May 2016 22:52:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-Needed-ASAP-Writing-Do-loop-with-dynamic-variables/m-p/267751#M52949</guid>
      <dc:creator>Kuda35</dc:creator>
      <dc:date>2016-05-02T22:52:45Z</dc:date>
    </item>
    <item>
      <title>Re: Help Needed ASAP -  Writing Do loop with dynamic variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-Needed-ASAP-Writing-Do-loop-with-dynamic-variables/m-p/267757#M52952</link>
      <description>&lt;P&gt;Why do you need help ASAP?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your mixing macro and data step logic. Use one or the other.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example you never use your i/j macro variables. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It also appears as if you've posted a portion of your code.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 May 2016 23:58:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-Needed-ASAP-Writing-Do-loop-with-dynamic-variables/m-p/267757#M52952</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-05-02T23:58:13Z</dc:date>
    </item>
    <item>
      <title>Re: Help Needed ASAP -  Writing Do loop with dynamic variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-Needed-ASAP-Writing-Do-loop-with-dynamic-variables/m-p/267775#M52954</link>
      <description>&lt;P&gt;Use &amp;amp;i and &amp;amp;j everywhere to refer to the macro variables created by your %do loops.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is not clear what you are trying to do. But I suspect that it doesn't require macro coding at all. Take a step back and describe the problem you are trying to solve.&lt;/P&gt;</description>
      <pubDate>Tue, 03 May 2016 02:41:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-Needed-ASAP-Writing-Do-loop-with-dynamic-variables/m-p/267775#M52954</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-05-03T02:41:55Z</dc:date>
    </item>
    <item>
      <title>Re: Help Needed ASAP -  Writing Do loop with dynamic variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-Needed-ASAP-Writing-Do-loop-with-dynamic-variables/m-p/267874#M52970</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As refered before&amp;nbsp;by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza﻿&lt;/a&gt;&amp;nbsp;your are&amp;nbsp;&lt;SPAN&gt;mixing macro and data step logic.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;But even without knowing a full picture of what you entend to do here some code based on your sample:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Some assumption:&lt;/STRONG&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;SPAN&gt;&lt;SPAN&gt;I don't have To_Date var so i used :&amp;nbsp;To_Date=today();&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;&lt;SPAN&gt;I don't know what you wan't as final result so the table Stuff is just also one asumption &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Some;

	format mmdate ddmmyy10.;
	length varname $10 ;
	TO_DATE=today();

	Do i = 2010 to 2010;
		Do j = 1 to 2;
			k = 1;
			mmdate=mdy(j,k,i);
			
			enddate = TO_DATE;
			eom=intnx( 'month', mmdate,0,'end');
			numdays=day(eom);
			x = 0;

			if mmdate&amp;gt;= start_date and mmdate&amp;lt;= enddate then
				x = numdays;

			if year(mmdate)= year(start_date) and month(mmdate)= month(start_date) then
				x = numdays - day(start_date);

			if year(mmdate)= year(enddate) and month(mmdate)= month(enddate) then
				x = day(enddate);
			VALUE = x;
&lt;BR /&gt;                        /* will be the future column name */
			VARNAME=CATT("EXP_",i,strip(put(j,z2.)));
			output;
		End;
	End;
run;
&lt;BR /&gt;/* using transpose to create the columns as defined on the varname variable */
proc transpose data=some
	out=stuff (drop=_:)	name=_source;
	id varname;
	var value;
run; quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Best regards.&lt;/P&gt;</description>
      <pubDate>Tue, 03 May 2016 11:00:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-Needed-ASAP-Writing-Do-loop-with-dynamic-variables/m-p/267874#M52970</guid>
      <dc:creator>BrunoSilva</dc:creator>
      <dc:date>2016-05-03T11:00:47Z</dc:date>
    </item>
    <item>
      <title>Re: Help Needed ASAP -  Writing Do loop with dynamic variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-Needed-ASAP-Writing-Do-loop-with-dynamic-variables/m-p/267956#M53000</link>
      <description>&lt;P&gt;Hi all, thanks for you replies so far.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To clarify, I have a table with data &amp;nbsp;membership on customers with From_date(start) and to_date(end), which can be over several years. I need to calculate : .&lt;/P&gt;&lt;P&gt;- &amp;nbsp;the member of days in each month that the person is a member.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To do this, I will would like to add a new column for each month 'EXPMMYYYY' to the dataset &amp;nbsp;and calculatte the membership days for each month. This code will form part of a process &amp;nbsp;that will be repeated on a monthly basis&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Becuase of this, I need &amp;nbsp;the looping code that allows me to add &amp;nbsp;the 'EXPMMYYYY' columns dynamically to the dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried the solutions suggested but that haven't quite work. &amp;nbsp;Apologies if I am missing something. I am new to SAS. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 May 2016 14:16:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-Needed-ASAP-Writing-Do-loop-with-dynamic-variables/m-p/267956#M53000</guid>
      <dc:creator>Kuda35</dc:creator>
      <dc:date>2016-05-03T14:16:29Z</dc:date>
    </item>
    <item>
      <title>Re: Help Needed ASAP -  Writing Do loop with dynamic variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-Needed-ASAP-Writing-Do-loop-with-dynamic-variables/m-p/267962#M53001</link>
      <description>&lt;P&gt;1. Post your full code&lt;/P&gt;
&lt;P&gt;2. Post sample data and expected output&lt;/P&gt;</description>
      <pubDate>Tue, 03 May 2016 14:24:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-Needed-ASAP-Writing-Do-loop-with-dynamic-variables/m-p/267962#M53001</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-05-03T14:24:50Z</dc:date>
    </item>
    <item>
      <title>Re: Help Needed ASAP -  Writing Do loop with dynamic variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-Needed-ASAP-Writing-Do-loop-with-dynamic-variables/m-p/267968#M53003</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83769"&gt;@Kuda35&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi all, thanks for you replies so far.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To do this, I will would like to add a new column for each month 'EXPMMYYYY' to the dataset &amp;nbsp;and calculatte the membership days for each month. This code will form part of a process &amp;nbsp;that will be repeated on a monthly basis&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You are likely better off having one record per&amp;nbsp;start and one with end&amp;nbsp;with a value to tell you which month it is and use SAS date values &lt;/P&gt;
&lt;P&gt;Adding new columns every month means that your data processing just gets more complicated every time you add a column(variable).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 May 2016 14:37:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-Needed-ASAP-Writing-Do-loop-with-dynamic-variables/m-p/267968#M53003</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-05-03T14:37:53Z</dc:date>
    </item>
    <item>
      <title>Re: Help Needed ASAP -  Writing Do loop with dynamic variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-Needed-ASAP-Writing-Do-loop-with-dynamic-variables/m-p/267979#M53004</link>
      <description>&lt;P&gt;Please find full code &amp;nbsp;and input and output data attached.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Libname worklib '/mnt/silverbhw/Common_Share/Temp/IDM/Work/';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data worklib.ExposureC;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; set worklib.ExposureB;&lt;/P&gt;&lt;P&gt;/* Exposure months*/&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;format mmdate ddmmyy10.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Do i = 2010 to 2010;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Do j = 1 to 2;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;k = 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;mmdate=mdy(j,k,i);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;output;&lt;BR /&gt;&lt;BR /&gt;start_date = FROM_DATE;&lt;BR /&gt;enddate = TO_DATE;&lt;BR /&gt;eom=intnx( 'month', mmdate,0,'end');&lt;BR /&gt;numdays=day(eom);&lt;BR /&gt;output;&lt;/P&gt;&lt;P&gt;x = 0;&lt;/P&gt;&lt;P&gt;if mmdate&amp;gt;= start_date and mmdate&amp;lt;= enddate then&lt;BR /&gt;x = numdays;&lt;/P&gt;&lt;P&gt;if year(mmdate)= year(start_date) and month(mmdate)= month(start_date) then&lt;BR /&gt;x = numdays - day(start_date);&lt;/P&gt;&lt;P&gt;if year(mmdate)= year(enddate) and month(mmdate)= month(enddate) then&lt;BR /&gt;x = day(enddate);&lt;BR /&gt;Varname = x;&lt;BR /&gt;output;&lt;BR /&gt;&lt;BR /&gt;End;&lt;BR /&gt;End;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 03 May 2016 15:10:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-Needed-ASAP-Writing-Do-loop-with-dynamic-variables/m-p/267979#M53004</guid>
      <dc:creator>Kuda35</dc:creator>
      <dc:date>2016-05-03T15:10:05Z</dc:date>
    </item>
    <item>
      <title>Re: Help Needed ASAP -  Writing Do loop with dynamic variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-Needed-ASAP-Writing-Do-loop-with-dynamic-variables/m-p/267981#M53005</link>
      <description>&lt;P&gt;Sorry, please see revised input/output file attached.&lt;/P&gt;</description>
      <pubDate>Tue, 03 May 2016 15:11:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-Needed-ASAP-Writing-Do-loop-with-dynamic-variables/m-p/267981#M53005</guid>
      <dc:creator>Kuda35</dc:creator>
      <dc:date>2016-05-03T15:11:46Z</dc:date>
    </item>
  </channel>
</rss>

