<?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: Count Variable inside Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-Variable-inside-Macro/m-p/131804#M26841</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Vincent, this is the sort of solution I was aiming for, and once I close the do loop, it works perfectly inside the macro!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 09 Sep 2013 15:44:47 GMT</pubDate>
    <dc:creator>moreka</dc:creator>
    <dc:date>2013-09-09T15:44:47Z</dc:date>
    <item>
      <title>Count Variable inside Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-Variable-inside-Macro/m-p/131799#M26836</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I would like to create a variable that increases by 1 only when another variable satisfies some criterion.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example, in drug1, the stopdates are at the 2rd, 4th, and 6th visits; whereas with drug2, the stopdates are at the 3rd and 5th visits, and the stopdate counts from 1-2 with a missing at 3 since there is no 3rd time stopping the drug.&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;The data should come out where:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;visitdate_1_2 = stopdate1_1&lt;/P&gt;&lt;P&gt;visitdate_1_4 = stopdate1_2&lt;/P&gt;&lt;P&gt;visitdate_1_6 = stopdate1_3&lt;/P&gt;&lt;P&gt;visitdate_2_3 = stopdate2_1&lt;/P&gt;&lt;P&gt;visitdate_2_5 = stopdate2_2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; . = stopdate2_3&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The macro below is what I'm working with.&amp;nbsp; The [??????] is where I'm guessing a macro variable or function might go.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro duration (n,m);&lt;/P&gt;&lt;P&gt;data two; set one;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%do i=1 %to &amp;amp;n.;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *drug number, up to 10;&lt;/P&gt;&lt;P&gt;%do j=1 %to &amp;amp;m.;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *changes to each drug, up to 6;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if changed&amp;amp;i._&amp;amp;j.='stop' then do stopdate&amp;amp;i._[???????]= (visitdate_&amp;amp;i._&amp;amp;j.);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%end; %end; &lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I attempted another do loop inside the other two, but it just wrote one date on top of the last, so they were all the same. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any suggestions?&amp;nbsp; Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 07 Sep 2013 18:52:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-Variable-inside-Macro/m-p/131799#M26836</guid>
      <dc:creator>moreka</dc:creator>
      <dc:date>2013-09-07T18:52:30Z</dc:date>
    </item>
    <item>
      <title>Re: Count Variable inside Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-Variable-inside-Macro/m-p/131800#M26837</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I could be wrong, but it looks like what you're trying to do has absolutely nothing to do with macros.&amp;nbsp; I would suggest studying the following DATA step statements (what they do, when they can be used):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;RETAIN &lt;/P&gt;&lt;P&gt;OUTPUT&lt;/P&gt;&lt;P&gt;BY&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It's possible that arrays might come into the picture as well.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As a general rule, macro language won't help you unless you can picture what the final program should look like after the macro language has done its work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 07 Sep 2013 19:37:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-Variable-inside-Macro/m-p/131800#M26837</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2013-09-07T19:37:22Z</dc:date>
    </item>
    <item>
      <title>Re: Count Variable inside Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-Variable-inside-Macro/m-p/131801#M26838</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I think you may be right that I can't solve this with a macro and an array may need to be used.&amp;nbsp; Though, since the dataset is in wide-form (like what's reflected in the macro above), I'm not sure if I would be able to sort on a specific variable or create a dataset based on a specific outcome, since the outcome of interest can occur multiple times and in any one of 60 variables per observation (6 drugs*10 changes).&amp;nbsp; Unless of course, I'm misunderstanding, which is entirely possible!&amp;nbsp; I've used similar macros to the one above on this dataset and have had a lot of success pulling out information, but this last problem has me stumped.&amp;nbsp; Thanks for your input!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 08 Sep 2013 20:08:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-Variable-inside-Macro/m-p/131801#M26838</guid>
      <dc:creator>moreka</dc:creator>
      <dc:date>2013-09-08T20:08:46Z</dc:date>
    </item>
    <item>
      <title>Re: Count Variable inside Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-Variable-inside-Macro/m-p/131802#M26839</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;BR /&gt;It would be possible in macros if you used fetch and the like to read your data steps through macros instead of via a data step but it would be much more of a hassle.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, you can thinker with the way your data is setup and indirectly increment with additionnal conditioning on your data. Since all of your data is within a single row you could do something like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro duration (n,m);&lt;/P&gt;&lt;P&gt;data two; set one;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%do i=1 %to &amp;amp;n.;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *drug number, up to 10;&lt;/P&gt;&lt;P&gt;%do j=1 %to &amp;amp;m.;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *changes to each drug, up to 6;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if changed&amp;amp;i._&amp;amp;j.='stop' and stopdate&amp;amp;i._1=. then do stopdate&amp;amp;i._1= (visitdate_&amp;amp;i._&amp;amp;j.);&lt;/P&gt;&lt;P&gt;%do k=2 %to 3;&lt;/P&gt;&lt;P&gt;else if changed&amp;amp;i._&amp;amp;j.='stop' and stopdate&amp;amp;i._&amp;amp;k.=. then do stopdate&amp;amp;i._&amp;amp;k.= (visitdate_&amp;amp;i._&amp;amp;j.);&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%end; %end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've assumed, since you can only have 6 changed to drugs, that the maximum number of times one could change drug would be of 3 hence the hard typed but for so long as you know a priori how many stopdate is the maximum you can get, you can change the upper bound of the loop on k.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Vincent&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 09 Sep 2013 11:59:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-Variable-inside-Macro/m-p/131802#M26839</guid>
      <dc:creator>Vince28_Statcan</dc:creator>
      <dc:date>2013-09-09T11:59:24Z</dc:date>
    </item>
    <item>
      <title>Re: Count Variable inside Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-Variable-inside-Macro/m-p/131803#M26840</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;data want ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; array sd1 stopdate1_1 - stopdate1_3 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; array sd2 stopdate2_1 - stopdate2_3 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; array vd1 visitdate_1_1 - visitdate_1_6 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; array vd2 visitdate_2_1 - visitdate_2_5 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; sd1(1) = vd1(2);&lt;/P&gt;&lt;P&gt;&amp;nbsp; sd1(2) = vd1(4);&lt;/P&gt;&lt;P&gt;&amp;nbsp; sd1(3) = vd1(6);&lt;/P&gt;&lt;P&gt;&amp;nbsp; sd2(1) = vd2(3);&lt;/P&gt;&lt;P&gt;&amp;nbsp; sd2(2) = vd2(5);&lt;/P&gt;&lt;P&gt;&amp;nbsp; sd2(3) = . ;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Not sure where macro logic comes into this problem.&amp;nbsp; Are you trying to find a generalized solution?&lt;/P&gt;&lt;P&gt;If so what is it that varies?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 09 Sep 2013 13:55:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-Variable-inside-Macro/m-p/131803#M26840</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2013-09-09T13:55:31Z</dc:date>
    </item>
    <item>
      <title>Re: Count Variable inside Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-Variable-inside-Macro/m-p/131804#M26841</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Vincent, this is the sort of solution I was aiming for, and once I close the do loop, it works perfectly inside the macro!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 09 Sep 2013 15:44:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-Variable-inside-Macro/m-p/131804#M26841</guid>
      <dc:creator>moreka</dc:creator>
      <dc:date>2013-09-09T15:44:47Z</dc:date>
    </item>
    <item>
      <title>Re: Count Variable inside Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-Variable-inside-Macro/m-p/131805#M26842</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Tom, I don't think I introduced this very clearly. &lt;/P&gt;&lt;P&gt;My data is in wide-form, with each drug, the changes for each drug and the dates for each change, all in one row.&amp;nbsp; &lt;/P&gt;&lt;P&gt;The macro I included is what I've frequently used to process other information for this dataset, so I was looking for something that would work in this context.&amp;nbsp; &lt;/P&gt;&lt;P&gt;Since any of the 60 date variables could be a potential stop date, I wanted to only pull out the visit dates on which the drugs were stopped and have those stop dates count sequentially.&lt;/P&gt;&lt;P&gt;Given that, it looks like the indirect incrementing inside the macro works to output specific dates I want to capture.&lt;/P&gt;&lt;P&gt;Thanks and sorry that wasn't more straightforward from the get-go!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 09 Sep 2013 16:00:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-Variable-inside-Macro/m-p/131805#M26842</guid>
      <dc:creator>moreka</dc:creator>
      <dc:date>2013-09-09T16:00:15Z</dc:date>
    </item>
  </channel>
</rss>

