<?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: Generate new variable in a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Generate-new-variable-in-a-macro/m-p/741165#M231653</link>
    <description>&lt;P&gt;If either of the variables might be missing and you still want the sum of what is actually present then use:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;%macro newvar (dataset, variable, suffix1, suffix2); 

	data want;
		set &amp;amp;dataset;
		&amp;amp;variable._sum= sum(&amp;amp;variable._&amp;amp;suffix1,&amp;amp;variable._&amp;amp;suffix2);
	run;
	
%mend newvar;&lt;/LI-CODE&gt;</description>
    <pubDate>Thu, 13 May 2021 16:32:12 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-05-13T16:32:12Z</dc:date>
    <item>
      <title>Generate new variable in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-new-variable-in-a-macro/m-p/741059#M231622</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Say I have a dataset with a couple of variables and I want to create a new variable being equal to sum of the two selected variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input year id sales_prod_A sales_prod_B sales_prod_C other_var;
	datalines;
	2010 1 100 80 100 500
	2010 2 200 130 100 600
	2010 3 300 50 200 500
	2011 1 150 75 300 600
	2011 2 275 150 400 200
	2011 3 405 50 500 300
	2012 1 190 200 600 400
	2012 2 280 140 100 200
	2012 3 420 70 200 600
;
run;
data want;
	set have;
	sales_prod_sum=sales_prod_A+sales_prod_B;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;Now, I want to do the same in the macro, so I try something like this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro newvar (dataset, variable, suffix1, suffix2); 

	data want;
		set &amp;amp;dataset;
		&amp;amp;variable_sum=&amp;amp;variable_&amp;amp;suffix1 + &amp;amp;variable_&amp;amp;suffix2;
	run;
	
%mend newvar;
%newvar (have, sales_prod, A, B);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Could you tell me what's wrong with the code?&lt;/P&gt;</description>
      <pubDate>Thu, 13 May 2021 11:14:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-new-variable-in-a-macro/m-p/741059#M231622</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2021-05-13T11:14:56Z</dc:date>
    </item>
    <item>
      <title>Re: Generate new variable in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-new-variable-in-a-macro/m-p/741060#M231623</link>
      <description>&lt;P&gt;To indicate the name of a macro variable has ended, you need to use a dot&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro newvar (dataset, variable, suffix1, suffix2); 

	data want;
		set &amp;amp;dataset;
		&amp;amp;variable._sum=&amp;amp;variable._&amp;amp;suffix1 + &amp;amp;variable._&amp;amp;suffix2;
	run;
	
%mend newvar;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you don't use the dot, then the SAS macro processor thinks that &amp;amp;variable_ (with an underscore at the end) is the name of the macro variable, and no such macro variable exists.&lt;/P&gt;</description>
      <pubDate>Thu, 13 May 2021 11:19:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-new-variable-in-a-macro/m-p/741060#M231623</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-05-13T11:19:35Z</dc:date>
    </item>
    <item>
      <title>Re: Generate new variable in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-new-variable-in-a-macro/m-p/741165#M231653</link>
      <description>&lt;P&gt;If either of the variables might be missing and you still want the sum of what is actually present then use:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;%macro newvar (dataset, variable, suffix1, suffix2); 

	data want;
		set &amp;amp;dataset;
		&amp;amp;variable._sum= sum(&amp;amp;variable._&amp;amp;suffix1,&amp;amp;variable._&amp;amp;suffix2);
	run;
	
%mend newvar;&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 13 May 2021 16:32:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-new-variable-in-a-macro/m-p/741165#M231653</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-05-13T16:32:12Z</dc:date>
    </item>
  </channel>
</rss>

