<?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: passing a numeric value in a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318735#M69872</link>
    <description>&lt;P&gt;It pains me to see folks generating macro loops when simple programming steps are easier to program&amp;nbsp;and debug.&amp;nbsp; And often faster.&amp;nbsp; If I understand your task correctly, that's what I think is happening to you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In a data step (data NEED), you can use array declarations, where the uppoer and lower bounds of the arrays are 1915:1974&amp;nbsp; and 1992:2014, as needed.&amp;nbsp; This program reads dataset HAVE, takes your list of CTFR variables (ctrf1915-ctrf1974), uses the corresponding MAGE value to pick out the needed RATES variable, and makes two new variables: CTRF_VAL and RATESEMF_VAL, for each iteration from 1915 through 1974.&amp;nbsp; So dataset NEED will have 60 records with two variables&amp;nbsp;generated from 1 record in dataset HAVE with 120 variables (ctrf1914-ctrf1974 and ratesemf_1992-ratesemf_2014).&amp;nbsp; NEED also has the years for CTRF and RATESEMF and also the MAGE value - identifying&amp;nbsp;which variable pair is produced.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then sort NEED by the CTRF year and RATESEMF year, and do a proc corr with a "by ctrf_year ratesemf_year".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "trick" here is to define arrays such that the first element is element number 1915 and the last is number 1974 for CTRF and MAGE.&amp;nbsp; And for the RATES array the lower and upper bounds are 1992 and 2014.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I understand your problem correctly, this program should do what you need:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Correction:&amp;nbsp; the statement&lt;/P&gt;
&lt;P&gt;&amp;nbsp; ratesemf_val=rat{mage_val};&lt;/P&gt;
&lt;P&gt;should be&lt;/P&gt;
&lt;P&gt;&amp;nbsp; ratesemf_val=rat{ratesemf_year};&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data need (keep=ctrf_val ctrf_year ratesemf_val ratesemf_year mage_val);
  set have;
  array ctf {1915:1974} ctrf1915-ctfr1974;            /* CTFR vars to correlate */
  array rat {1992:2014} ratesemf_1992-ratesemf_2014;  /* RATE vars to search for the correlated value*/
  array mag {1915:1974} mage1915-mage1974;            /* Offset to find the needed RATE var */

  do ctrfyear=lbound(ctf) to hbound(ctf);
    ctrf_val=ctf{ctrfyear};
	mage_val=mag{ctrfyear};
	ratesemf_year=ctrfyear+mage_val;
    ratesemf_val=rat{mage_val};
	output;
  end;
run;
proc sort data=need;
  by ctrf_year ratesemf_year;
run;
proc corr data=need noprint  out=ctf_rat_corrs_by_offset;
  by ctrf_year ratesemf_year;
  var ctrf_val;
  with ratesemf_val;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 14 Dec 2016 00:43:27 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2016-12-14T00:43:27Z</dc:date>
    <item>
      <title>passing a numeric value in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318706#M69857</link>
      <description>&lt;P&gt;I'm hoping someone can help me debug this code (in SAS 9.3).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to do a series of correlations between pairs of variables (ctfr1915-ctfr1974 and ratesemf_1992-ratesemf_2014) for multiple years using a third variable (mage1915-mage1974) to make the link. For example, mage1970=29 so I want to correlate ctfr1970 and ratesemf_1999 (1970+29=1999).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been using the code below and it is obviously getting hung up on the 5th line because ma is a character. How can I pass the numeric value so that it will put whatever the value of ma1965 is into the code and give me in the index year for the second variable (ratesemf_)? I've also pasted the log output below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;options SYMBOLGEN MPRINT;&lt;BR /&gt;%macro cohhous(fert, house,sy, ey);&lt;BR /&gt;%do yf=&amp;amp;sy. %to &amp;amp;ey.;&lt;BR /&gt;%let ma=mage&amp;amp;sy.;&lt;BR /&gt;%let yh=%eval(ma+&amp;amp;sy.);&lt;BR /&gt;proc corr data = temp1 outp=out&amp;amp;yf. ;&lt;BR /&gt;&amp;nbsp; var &amp;amp;fert.&amp;amp;yf. &amp;amp;house.&amp;amp;yh.;&lt;BR /&gt;data outb&amp;amp;yf.;&lt;BR /&gt;set out&amp;amp;yf.;&lt;BR /&gt;corr=&amp;amp;fert.&amp;amp;yf.;&lt;BR /&gt;year=&amp;amp;yf.;&lt;BR /&gt;keep year corr ;&lt;BR /&gt;if _n_=5;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;BR /&gt;%mend;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;%cohhous (ctfr,ratesemf_,1965,1974);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;671&amp;nbsp;&amp;nbsp; options SYMBOLGEN MPRINT;&lt;BR /&gt;672&amp;nbsp;&amp;nbsp; %macro cohhous(fert, house,sy, ey);&lt;BR /&gt;673&amp;nbsp;&amp;nbsp; %do yf=&amp;amp;sy. %to &amp;amp;ey.;&lt;BR /&gt;674&amp;nbsp;&amp;nbsp; %let ma=mage&amp;amp;sy.;&lt;BR /&gt;675&amp;nbsp;&amp;nbsp; %let yh=%eval(ma+&amp;amp;sy.);&lt;BR /&gt;676&amp;nbsp;&amp;nbsp; proc corr data = temp1 outp=out&amp;amp;yf. ;&lt;BR /&gt;677&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var &amp;amp;fert.&amp;amp;yf. &amp;amp;house.&amp;amp;yh.;&lt;BR /&gt;678&amp;nbsp;&amp;nbsp; data outb&amp;amp;yf.;&lt;BR /&gt;679&amp;nbsp;&amp;nbsp; set out&amp;amp;yf.;&lt;BR /&gt;680&amp;nbsp;&amp;nbsp; corr=&amp;amp;fert.&amp;amp;yf.;&lt;BR /&gt;681&amp;nbsp;&amp;nbsp; year=&amp;amp;yf.;&lt;BR /&gt;682&amp;nbsp;&amp;nbsp; keep year corr ;&lt;BR /&gt;683&amp;nbsp;&amp;nbsp; if _n_=5;&lt;BR /&gt;684&amp;nbsp;&amp;nbsp; run;&lt;BR /&gt;685&amp;nbsp;&amp;nbsp; %end;&lt;BR /&gt;686&amp;nbsp;&amp;nbsp; %mend;&lt;BR /&gt;687&amp;nbsp;&amp;nbsp; run;&lt;BR /&gt;688&lt;BR /&gt;689&amp;nbsp;&amp;nbsp; %cohhous (ctfr,ratesemf_,1965,1974);&lt;BR /&gt;SYMBOLGEN:&amp;nbsp; Macro variable SY resolves to 1965&lt;BR /&gt;SYMBOLGEN:&amp;nbsp; Macro variable EY resolves to 1974&lt;BR /&gt;SYMBOLGEN:&amp;nbsp; Macro variable SY resolves to 1965&lt;BR /&gt;SYMBOLGEN:&amp;nbsp; Macro variable SY resolves to 1965&lt;BR /&gt;ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; operand is required. The condition was: ma+1965&lt;BR /&gt;ERROR: The macro COHHOUS will stop executing.&lt;BR /&gt;690&amp;nbsp;&amp;nbsp; run;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Dec 2016 21:45:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318706#M69857</guid>
      <dc:creator>srbotto</dc:creator>
      <dc:date>2016-12-13T21:45:48Z</dc:date>
    </item>
    <item>
      <title>Re: passing a numeric value in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318709#M69859</link>
      <description>&lt;P&gt;It's not clear where the values for &amp;amp;YH are supposed to come from.&amp;nbsp; If they are in a SAS data set, you need to replace the %LET statement with a DATA step.&amp;nbsp; Within&amp;nbsp;a DATA step, CALL SYMPUTX allows you to create a macro variable by copying over the values found within a SAS data set.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the value for &amp;amp;YH is not supposed to come from a SAS data set, then what would be the proper value?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Dec 2016 21:55:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318709#M69859</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-12-13T21:55:26Z</dc:date>
    </item>
    <item>
      <title>Re: passing a numeric value in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318712#M69860</link>
      <description>&lt;P&gt;I guess there are (at least) two problems with the code then.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;amp;yh is what I'm hoping to generate by adding the value of mage to the suffix of ctfr. So in the case when I have the variables ctfr1970 and mage1970 which has the value 29 I want to call ratesemf_1999 I was hoping to create a variable, yh, that was equal to 1970+29. Does that make sense? (I hadn't even realized this error in the code because I couldn't get yh to calculate).&lt;/P&gt;</description>
      <pubDate>Tue, 13 Dec 2016 22:01:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318712#M69860</guid>
      <dc:creator>srbotto</dc:creator>
      <dc:date>2016-12-13T22:01:50Z</dc:date>
    </item>
    <item>
      <title>Re: passing a numeric value in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318716#M69863</link>
      <description>&lt;P&gt;Where does the "value of mage" come from?&lt;/P&gt;</description>
      <pubDate>Tue, 13 Dec 2016 22:06:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318716#M69863</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-12-13T22:06:07Z</dc:date>
    </item>
    <item>
      <title>Re: passing a numeric value in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318729#M69870</link>
      <description>&lt;P&gt;I suspect that you may be better off looking at listing all of variable as VAR and&amp;nbsp;all the others as WITH variables and sending the output to a single data set. Then you will have one set in the values of _name_ and the others in colum headings for the correlations. Then pull out the specific pairs you are concerned with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Dec 2016 23:11:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318729#M69870</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-12-13T23:11:15Z</dc:date>
    </item>
    <item>
      <title>Re: passing a numeric value in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318735#M69872</link>
      <description>&lt;P&gt;It pains me to see folks generating macro loops when simple programming steps are easier to program&amp;nbsp;and debug.&amp;nbsp; And often faster.&amp;nbsp; If I understand your task correctly, that's what I think is happening to you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In a data step (data NEED), you can use array declarations, where the uppoer and lower bounds of the arrays are 1915:1974&amp;nbsp; and 1992:2014, as needed.&amp;nbsp; This program reads dataset HAVE, takes your list of CTFR variables (ctrf1915-ctrf1974), uses the corresponding MAGE value to pick out the needed RATES variable, and makes two new variables: CTRF_VAL and RATESEMF_VAL, for each iteration from 1915 through 1974.&amp;nbsp; So dataset NEED will have 60 records with two variables&amp;nbsp;generated from 1 record in dataset HAVE with 120 variables (ctrf1914-ctrf1974 and ratesemf_1992-ratesemf_2014).&amp;nbsp; NEED also has the years for CTRF and RATESEMF and also the MAGE value - identifying&amp;nbsp;which variable pair is produced.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then sort NEED by the CTRF year and RATESEMF year, and do a proc corr with a "by ctrf_year ratesemf_year".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "trick" here is to define arrays such that the first element is element number 1915 and the last is number 1974 for CTRF and MAGE.&amp;nbsp; And for the RATES array the lower and upper bounds are 1992 and 2014.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I understand your problem correctly, this program should do what you need:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Correction:&amp;nbsp; the statement&lt;/P&gt;
&lt;P&gt;&amp;nbsp; ratesemf_val=rat{mage_val};&lt;/P&gt;
&lt;P&gt;should be&lt;/P&gt;
&lt;P&gt;&amp;nbsp; ratesemf_val=rat{ratesemf_year};&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data need (keep=ctrf_val ctrf_year ratesemf_val ratesemf_year mage_val);
  set have;
  array ctf {1915:1974} ctrf1915-ctfr1974;            /* CTFR vars to correlate */
  array rat {1992:2014} ratesemf_1992-ratesemf_2014;  /* RATE vars to search for the correlated value*/
  array mag {1915:1974} mage1915-mage1974;            /* Offset to find the needed RATE var */

  do ctrfyear=lbound(ctf) to hbound(ctf);
    ctrf_val=ctf{ctrfyear};
	mage_val=mag{ctrfyear};
	ratesemf_year=ctrfyear+mage_val;
    ratesemf_val=rat{mage_val};
	output;
  end;
run;
proc sort data=need;
  by ctrf_year ratesemf_year;
run;
proc corr data=need noprint  out=ctf_rat_corrs_by_offset;
  by ctrf_year ratesemf_year;
  var ctrf_val;
  with ratesemf_val;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Dec 2016 00:43:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318735#M69872</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2016-12-14T00:43:27Z</dc:date>
    </item>
    <item>
      <title>Re: passing a numeric value in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318767#M69884</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let ma=mage&amp;amp;sy.;
%let yh=%eval(ma+&amp;amp;sy.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In the %eval, you use the &lt;STRONG&gt;&lt;U&gt;text&lt;/U&gt;&lt;/STRONG&gt;&lt;U&gt;&lt;/U&gt; ma. I guess you rather wanted to use the macro variable reference &amp;amp;ma instead.&lt;/P&gt;
&lt;P&gt;But that doesn't make it any better.&lt;/P&gt;
&lt;P&gt;In the %let, you assign the text string (assuming that &amp;amp;sy=1965) mage1965 to ma, which once again is not a numerical value that the %eval can deal with.&lt;/P&gt;
&lt;P&gt;Make sure that you reference macro variables correctly, and keep in mind that data step variables are never present when macro triggers are resolved.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Dec 2016 07:42:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318767#M69884</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-12-14T07:42:16Z</dc:date>
    </item>
    <item>
      <title>Re: passing a numeric value in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318882#M69925</link>
      <description>&lt;P&gt;mage1915-mage1974 are all in the dataset.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Dec 2016 12:26:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318882#M69925</guid>
      <dc:creator>srbotto</dc:creator>
      <dc:date>2016-12-14T12:26:25Z</dc:date>
    </item>
    <item>
      <title>Re: passing a numeric value in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318885#M69926</link>
      <description>&lt;P&gt;With this approach I still need to find a way to get the correct index variable for the second group of variables (e.g. the WITH variables). Any advice or sample code to help with that?&lt;/P&gt;</description>
      <pubDate>Wed, 14 Dec 2016 12:28:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318885#M69926</guid>
      <dc:creator>srbotto</dc:creator>
      <dc:date>2016-12-14T12:28:41Z</dc:date>
    </item>
    <item>
      <title>Re: passing a numeric value in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318887#M69927</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro cohhous(fert, house,sy, ey);
%do yf=&amp;amp;sy. %to &amp;amp;ey.;
%let ma=mage&amp;amp;sy.;
%let yh=%eval(ma+&amp;amp;sy.); /* This statement happens outside of any data or proc step! */
proc corr data = temp1 outp=out&amp;amp;yf. ;
  var &amp;amp;fert.&amp;amp;yf. &amp;amp;house.&amp;amp;yh.;
data outb&amp;amp;yf.;
set out&amp;amp;yf.;
corr=&amp;amp;fert.&amp;amp;yf.;
year=&amp;amp;yf.;
keep year corr ;
if _n_=5;
run;
%end;
%mend;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As I told you before, you CANNOT access data step variables in macro language.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Dec 2016 12:31:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/318887#M69927</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-12-14T12:31:09Z</dc:date>
    </item>
    <item>
      <title>Re: passing a numeric value in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/319994#M70389</link>
      <description>&lt;P&gt;Thank you everyone. I had been hoping macro language would have made the task possible without brute-forcing it in a datastep, but it seems the datastep solution offered is the fastest way for me to get this code working.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Dec 2016 18:48:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/passing-a-numeric-value-in-a-macro/m-p/319994#M70389</guid>
      <dc:creator>srbotto</dc:creator>
      <dc:date>2016-12-19T18:48:06Z</dc:date>
    </item>
  </channel>
</rss>

