<?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: Dynamic macro question in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49868#M10388</link>
    <description>You could always just pass a string and parse if with %scan.&lt;BR /&gt;
&lt;BR /&gt;
Art</description>
    <pubDate>Wed, 15 Dec 2010 02:04:31 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2010-12-15T02:04:31Z</dc:date>
    <item>
      <title>Dynamic macro question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49867#M10387</link>
      <description>Hi all,&lt;BR /&gt;
&lt;BR /&gt;
I have a relatively simple dataset: basically it's just a list of variable names and their numeric values.  I'm trying to find a good macro/approach to adding to this dataset new entries whose values are multiples of specified variables in the database.&lt;BR /&gt;
&lt;BR /&gt;
E.g.&lt;BR /&gt;
&lt;BR /&gt;
Name  |  Value&lt;BR /&gt;
Strength = 9&lt;BR /&gt;
Dexterity = 4&lt;BR /&gt;
Constitution = 15&lt;BR /&gt;
Intelligence = 12&lt;BR /&gt;
&lt;BR /&gt;
etc.&lt;BR /&gt;
&lt;BR /&gt;
I want to be able to create "New Variable" with a value of Strength x Dexterity (or whatever)&lt;BR /&gt;
&lt;BR /&gt;
The situation might seem simple enough, but both the values and the variables I'm picking are going to change a lot, and with many iterations, and it would be much easier to dynamically calculate the new variables rather than entering them manually in a data step.  A macro seems to make the most sense, but I'm not sure how I can pass varying numbers of parameters to such a macro (e.g. sometimes I only want 2 variables multiplied, but sometimes it will be 3 or more).&lt;BR /&gt;
&lt;BR /&gt;
Any thoughts on this?  (Even a way to use a dynamic number of parameters in a macro would be a huge help!)</description>
      <pubDate>Tue, 14 Dec 2010 22:14:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49867#M10387</guid>
      <dc:creator>GVeers</dc:creator>
      <dc:date>2010-12-14T22:14:38Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic macro question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49868#M10388</link>
      <description>You could always just pass a string and parse if with %scan.&lt;BR /&gt;
&lt;BR /&gt;
Art</description>
      <pubDate>Wed, 15 Dec 2010 02:04:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49868#M10388</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2010-12-15T02:04:31Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic macro question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49869#M10389</link>
      <description>For a varying number of macro parameters you can also take advantage of the /PARMBUFF option on the %MACRO statement.  You will still however need to do some parsing, probably with %SCAN or %QSCAN.</description>
      <pubDate>Wed, 15 Dec 2010 02:58:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49869#M10389</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2010-12-15T02:58:35Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic macro question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49870#M10390</link>
      <description>For your formulas, will they need be adjustable at the observation level or at the DATA step level?  It makes quite a bit of difference on the complexity of the coding.  It is likely that the interrelationship of these variables will also be changing, how is the relationship defined/controlled?</description>
      <pubDate>Wed, 15 Dec 2010 03:02:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49870#M10390</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2010-12-15T03:02:11Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic macro question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49871#M10391</link>
      <description>Hello GVeers,&lt;BR /&gt;
&lt;BR /&gt;
If I understand you correctly then this is a solution:&lt;BR /&gt;
[pre]&lt;BR /&gt;
data i;&lt;BR /&gt;
  length Name $100;&lt;BR /&gt;
  input Name $ Value;&lt;BR /&gt;
datalines;&lt;BR /&gt;
Strength 9&lt;BR /&gt;
Dexterity 4&lt;BR /&gt;
Constitution 15&lt;BR /&gt;
Intelligence 12&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
%macro a(operation);&lt;BR /&gt;
  %let i=0;&lt;BR /&gt;
  %do %until (&amp;amp;&amp;amp;v&amp;amp;i = );&lt;BR /&gt;
     %let i=%EVAL(&amp;amp;i+1);&lt;BR /&gt;
     %let v&amp;amp;i=%SCAN(&amp;amp;operation,&amp;amp;i,"*");&lt;BR /&gt;
  %end;&lt;BR /&gt;
  %let n=%EVAL(&amp;amp;i-1);&lt;BR /&gt;
  %put v1=&amp;amp;v1 v&amp;amp;n=&amp;amp;&amp;amp;v&amp;amp;n;&lt;BR /&gt;
  data t;&lt;BR /&gt;
    merge &lt;BR /&gt;
    %let nv=;&lt;BR /&gt;
    %let vv=;&lt;BR /&gt;
    %do i=1 %to &amp;amp;n; &lt;BR /&gt;
       %let nv=&amp;amp;nv._&amp;amp;&amp;amp;v&amp;amp;i;&lt;BR /&gt;
       %let vv=&amp;amp;vv.*val&amp;amp;i;&lt;BR /&gt;
       i (rename=(name=nam&amp;amp;i value=val&amp;amp;i) where=(UPCASE(nam&amp;amp;i)=UPCASE("&amp;amp;&amp;amp;v&amp;amp;i")))&lt;BR /&gt;
    %end;&lt;BR /&gt;
    ;&lt;BR /&gt;
    %let nv=%SUBSTR(&amp;amp;nv,2);&lt;BR /&gt;
    %let vv=%SUBSTR(&amp;amp;vv,2);&lt;BR /&gt;
    %put nv=&amp;amp;nv vv=&amp;amp;vv;&lt;BR /&gt;
    name="&amp;amp;nv";&lt;BR /&gt;
    value=&amp;amp;vv;&lt;BR /&gt;
    keep name value;&lt;BR /&gt;
  run;&lt;BR /&gt;
  data r;&lt;BR /&gt;
    set i t;  &lt;BR /&gt;
  run; &lt;BR /&gt;
%mend a;&lt;BR /&gt;
%a(Dexterity*Constitution*Strength) &lt;BR /&gt;
[/pre]&lt;BR /&gt;
Sincerely,&lt;BR /&gt;
SPR</description>
      <pubDate>Wed, 15 Dec 2010 16:29:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49871#M10391</guid>
      <dc:creator>SPR</dc:creator>
      <dc:date>2010-12-15T16:29:57Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic macro question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49872#M10392</link>
      <description>Here is an alternative solution, although this macro probably still needs some work to make it more robust.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data test;  &lt;BR /&gt;
length Name $100;  &lt;BR /&gt;
input Name $ Value;&lt;BR /&gt;
datalines;&lt;BR /&gt;
Strength 9&lt;BR /&gt;
Dexterity 4&lt;BR /&gt;
Constitution 15&lt;BR /&gt;
Intelligence 12&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
%macro multiply(names);&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
	select distinct value into: expression	separated by '*'&lt;BR /&gt;
	from test&lt;BR /&gt;
	where find("&amp;amp;names", trim(name)) &amp;gt; 0;&lt;BR /&gt;
quit;&lt;BR /&gt;
data test;&lt;BR /&gt;
	if 0 then modify test;&lt;BR /&gt;
	name = tranwrd("&amp;amp;names", ' ', ' x ');&lt;BR /&gt;
	value = %sysevalf(&amp;amp;expression);&lt;BR /&gt;
	output;&lt;BR /&gt;
run;&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
If you invoke this macro using %multiply(Strength Intelligence) and print the test data set, here are the results:&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
                             Obs    Name                       Value&lt;BR /&gt;
&lt;BR /&gt;
                              1     Strength                      9&lt;BR /&gt;
                              2     Dexterity                     4&lt;BR /&gt;
                              3     Constitution                 15&lt;BR /&gt;
                              4     Intelligence                 12&lt;BR /&gt;
                              5     Strength x Intelligence     108&lt;BR /&gt;
[/pre]</description>
      <pubDate>Wed, 15 Dec 2010 18:51:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49872#M10392</guid>
      <dc:creator>polingjw</dc:creator>
      <dc:date>2010-12-15T18:51:35Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic macro question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49873#M10393</link>
      <description>Hi.polingjw  &lt;BR /&gt;
The code can change somewhat.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Sorry.I am wrong.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp

Message was edited by: Ksharp</description>
      <pubDate>Thu, 16 Dec 2010 08:58:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49873#M10393</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2010-12-16T08:58:38Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic macro question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49874#M10394</link>
      <description>Thanks for everyone's help.  I realized after my post that I over-simplified my problem, but nonetheless I am most of the way there thanks to the code above.&lt;BR /&gt;
&lt;BR /&gt;
The last thing I'm now trying to figure out is how to use the scan function to return a variable name (rather than a string value) within my macro.  My snippet of code looks something like this:&lt;BR /&gt;
&lt;BR /&gt;
&amp;amp;newvar = scan("&amp;amp;string_vals", i) * scan(&amp;amp;string_vars, i);&lt;BR /&gt;
&lt;BR /&gt;
Within the macro, &amp;amp;newvar resolves to a variable in the dataset (good) and the first scan statement resolves to a value from a string of values (also good).  But the second scan statement is returning a string value when I want SAS to interpret it as a variable name.  What is the magic word to do such a thing?  I have messed with quotes and ampersands but can't seem to get it to work.</description>
      <pubDate>Thu, 16 Dec 2010 19:01:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49874#M10394</guid>
      <dc:creator>GVeers</dc:creator>
      <dc:date>2010-12-16T19:01:38Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic macro question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49875#M10395</link>
      <description>For reference, here is my macro code.  The basic idea is that it generates a list of desired values (or coefficients) from one data set, then in the second data set multiplies all the matching variables by their coefficients and adds them together into a single variable.  It doesn't quite work yet, but I think I could make it to work if I understood how I can force scan to see a variable name instead of a string.  The underlined part is what I'm referencing in the previous post.&lt;BR /&gt;
&lt;BR /&gt;
%macro freezer (data=, coefficients=, varcol=, valcol=, variables=, combined=, outdata=);&lt;BR /&gt;
/* This is pretty much polingjw's code and puts a list of desired values separated by spaces into "expression" */&lt;BR /&gt;
	proc sql noprint;&lt;BR /&gt;
		select distinct &amp;amp;valcol into: expression separated by ' '&lt;BR /&gt;
		from &amp;amp;coefficients&lt;BR /&gt;
		where find("&amp;amp;variables", trim(&amp;amp;varcol)) &amp;gt; 0;&lt;BR /&gt;
	quit;&lt;BR /&gt;
/* end polingjw's code */&lt;BR /&gt;
&lt;BR /&gt;
/* set out data set to data set */&lt;BR /&gt;
	data &amp;amp;outdata; set &amp;amp;data;&lt;BR /&gt;
		&amp;amp;combined = 0;&lt;BR /&gt;
		i = 1;&lt;BR /&gt;
	run;&lt;BR /&gt;
/* create new variable that multipies desired variables by their matching coefficients */&lt;BR /&gt;
	data &amp;amp;outdata; set &amp;amp;outdata;&lt;BR /&gt;
		do until(scan(expression, i) = ' ');&lt;BR /&gt;
			&lt;U&gt;&amp;amp;combined = &amp;amp;combined + scan(expression, i) * scan("&amp;amp;variables", i);&lt;/U&gt;			i+1;&lt;BR /&gt;
			output;&lt;BR /&gt;
		end;&lt;BR /&gt;
		drop i expression;&lt;BR /&gt;
	run;&lt;BR /&gt;
%mend freezer;</description>
      <pubDate>Thu, 16 Dec 2010 19:08:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49875#M10395</guid>
      <dc:creator>GVeers</dc:creator>
      <dc:date>2010-12-16T19:08:17Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic macro question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49876#M10396</link>
      <description>Almost certainly these will need to be %SCAN or %QSCAN functions and probably inside of a macro loop (making the word number &amp;amp;I).&lt;BR /&gt;
&lt;BR /&gt;
&amp;gt; &amp;amp;newvar = scan("&amp;amp;string_vals", i) *&lt;BR /&gt;
&amp;gt; scan(&amp;amp;string_vars, i);&lt;BR /&gt;
&lt;BR /&gt;
It is a problem of timing. The macro language writes the assignment statement that will be compiled in the DATA step.</description>
      <pubDate>Thu, 16 Dec 2010 19:17:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-macro-question/m-p/49876#M10396</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2010-12-16T19:17:31Z</dc:date>
    </item>
  </channel>
</rss>

