<?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: Using a macro variable to comput in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-variable-to-compute/m-p/278898#M56154</link>
    <description>&lt;P&gt;No need for macro coding. Take advantage of array and function vname:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
set test;
array v Q1--Q3;
do i = 1 to dim(v);
    if findw(string, vname(v{i})) &amp;gt; 0 then score = sum(score, v{i});
    end;
drop i;
run;

proc print noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 21 Jun 2016 02:27:59 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2016-06-21T02:27:59Z</dc:date>
    <item>
      <title>Using a macro variable to compute</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-variable-to-compute/m-p/278890#M56150</link>
      <description>&lt;P&gt;&lt;FONT size="3"&gt;Hello, &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="3"&gt;I have a dataset (Data1) like as below. The variable string indicates how to sum up Q1 ~ Q3. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="arial black,avant garde" size="3"&gt;ID Q1 Q2 Q3&amp;nbsp; string&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" size="3"&gt;1&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sum(Q1, Q3)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="3"&gt;&lt;FONT color="#0000ff"&gt;2&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sum(Q1, Q2, Q3)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" size="3"&gt;3&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sum(Q2, Q3)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" size="3"&gt;Now I would like to create a new numerical variable &lt;FONT color="#800000" face="arial black,avant garde"&gt;Score&lt;/FONT&gt; which is sumed as &lt;FONT face="arial black,avant garde"&gt;string &lt;FONT face="arial,helvetica,sans-serif"&gt;text:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="arial black,avant garde" size="3"&gt;ID Q1 Q2 Q3&amp;nbsp; string&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; &lt;FONT color="#800000"&gt;Score&lt;/FONT&gt; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" size="3"&gt;1&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sum(Q1, Q3)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color="#800000"&gt;2&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" size="3"&gt;2&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sum(Q1, Q2, Q3)&amp;nbsp; &lt;FONT color="#800000"&gt;7&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" size="3"&gt;3&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sum(Q2, Q3)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color="#800000"&gt;5&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" size="3"&gt;My code:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" face="courier new,courier" size="3"&gt;Data _null_;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" face="courier new,courier" size="3"&gt;set Data1;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" face="courier new,courier" size="3"&gt;call symput('n' !! strip(_n_), string);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" face="courier new,courier" size="3"&gt;call symput('total' , _n_);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" face="courier new,courier" size="3"&gt;Run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" face="courier new,courier" size="3"&gt;%macro test;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" face="courier new,courier" size="3"&gt;Data compute;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" face="courier new,courier" size="3"&gt;set Data1;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" face="courier new,courier" size="3"&gt;%Do i = 1 %to &amp;amp;total;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" face="courier new,courier" size="3"&gt;Score = &amp;amp;&amp;amp;n&amp;amp;i;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" face="courier new,courier" size="3"&gt;%End;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" face="courier new,courier" size="3"&gt;Run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" face="courier new,courier" size="3"&gt;%mend;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" face="courier new,courier" size="3"&gt;%test;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" size="3"&gt;But a error message keeps saying&amp;nbsp;"Syntax error". Does anyone know what the problem is? How to modify my code? Thanks.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" size="3"&gt;ps.The log window is attached below.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/12675i78F414E152A48E6B/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="Error msg.png" title="Error msg.png" /&gt;</description>
      <pubDate>Wed, 06 Jul 2016 01:56:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-variable-to-compute/m-p/278890#M56150</guid>
      <dc:creator>csfcgua</dc:creator>
      <dc:date>2016-07-06T01:56:34Z</dc:date>
    </item>
    <item>
      <title>Re: Using a macro variable to comput</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-variable-to-compute/m-p/278894#M56152</link>
      <description>&lt;P&gt;Take a look through your data set for the values of STRING. &amp;nbsp;It's possible that one of the values doesn't form a valid SAS statement. &amp;nbsp;In particular, if STRING is blank, this generated statement&amp;nbsp;might result in the message you're seeing (can't test it right now):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Score = ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At any rate, even if the program ran without error it would generate the wrong answer. &amp;nbsp;You would get a DATA step that looked like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data compute;&lt;/P&gt;
&lt;P&gt;set Data1;&lt;/P&gt;
&lt;P&gt;Score = sum(Q1, Q3);&lt;/P&gt;
&lt;P&gt;Score = sum(Q1, Q2, Q3);&lt;/P&gt;
&lt;P&gt;Score = sum(Q2, Q3);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Clearly, if you saw that data step without macro language, you would realize that the logic is incorrect.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try it this way (again, I can't test it right now):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data compute;&lt;/P&gt;
&lt;P&gt;set Data1;&lt;/P&gt;
&lt;P&gt;if string &amp;gt; ' ' then score = resolve(string);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it works, you're home free with no macro complications.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jun 2016 02:15:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-variable-to-compute/m-p/278894#M56152</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-06-21T02:15:07Z</dc:date>
    </item>
    <item>
      <title>Re: Using a macro variable to comput</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-variable-to-compute/m-p/278898#M56154</link>
      <description>&lt;P&gt;No need for macro coding. Take advantage of array and function vname:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
set test;
array v Q1--Q3;
do i = 1 to dim(v);
    if findw(string, vname(v{i})) &amp;gt; 0 then score = sum(score, v{i});
    end;
drop i;
run;

proc print noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Jun 2016 02:27:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-variable-to-compute/m-p/278898#M56154</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-06-21T02:27:59Z</dc:date>
    </item>
    <item>
      <title>Re: Using a macro variable to comput</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-variable-to-compute/m-p/278902#M56156</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding﻿&lt;/a&gt;&amp;nbsp;Thank you. I checked my data and removed the rows&amp;nbsp;with blank string.&amp;nbsp;Everything works now!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats﻿&lt;/a&gt;&amp;nbsp;Your simple way is good. Save me a lot of time. Thanks.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jun 2016 02:38:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-variable-to-compute/m-p/278902#M56156</guid>
      <dc:creator>csfcgua</dc:creator>
      <dc:date>2016-06-21T02:38:54Z</dc:date>
    </item>
    <item>
      <title>Re: Using a macro variable to comput</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-variable-to-compute/m-p/278904#M56157</link>
      <description>&lt;P&gt;You need to do some code generation. I find it easiest to do this by using a data step to write the code to a file and then using %INCLUDE to run the generated code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First get the distinct values of STRING and then generate the IF/THEN (or SELECT if you want) statements. &amp;nbsp;Then run them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  infile cards dsd dlm='|' truncover ;
  length id q1-q3 8 string $30 expected_score 8 ;
  input id -- expected_score ;
cards;
1|1|3|1|sum(Q1, Q3)|2
2|3|2|2|sum(Q1, Q2, Q3)|7
3|2|1|4|sum(Q2, Q3)|5
;;;;

proc sort data=have (keep=string) out=strings nodupkey ;
  by string;
run;

filename code temp;
data _null_;
  set strings ;
  file code;
  if _n_ &amp;gt; 1 then put 'else ' @;
  put 'if ' string = :$quote. 'then score=' string ';' ;
run;

data want ;
  set have ;
%include code / source2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Jun 2016 02:42:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-variable-to-compute/m-p/278904#M56157</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-06-21T02:42:43Z</dc:date>
    </item>
    <item>
      <title>Re: Using a macro variable to comput</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-variable-to-compute/m-p/278907#M56158</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  infile cards dsd dlm='|' truncover ;
  length id q1-q3 8 string $30 expected_score 8 ;
  input id -- expected_score ;
cards;
1|1|3|1|sum(Q1, Q3)|2
2|3|2|2|sum(Q1, Q2, Q3)|7
3|2|1|4|sum(Q2, Q3)|5
;;;;
;
data want;
 set have; 
 array x{*} q1-q3;
 length temp $ 2000;
 temp=string;
 do i=1 to dim(x);
  temp=tranwrd(temp,upcase(vname(x{i})),strip(x{i}));
 end;
 want=resolve(cats('%sysfunc(',temp,')'));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Jun 2016 03:17:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-variable-to-compute/m-p/278907#M56158</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-06-21T03:17:09Z</dc:date>
    </item>
  </channel>
</rss>

