<?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: how to insert a macro variable value as a statement in data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347109#M273270</link>
    <description>&lt;P&gt;If you want to use the value of a macro variable as a statement in a data step then just do that. &amp;nbsp;Macro variable resolution is just simple text substitution.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mvar=x2 = x**2 ;
data want;
  input x;
  &amp;amp;mvar ;
cards;
1
2
3
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But if you want to generate a lot of code from a data set then I would avoid macro variables and just write the code to file. &amp;nbsp;You can then use %INCLUDE to place it where you want it. &amp;nbsp;This has the advantages of avoiding macro variables and worries of macro quoting and also you can look at the file after it is generated and debug the code generating step more easily.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
   set my_metadata ;
   put target '=' expression ';' ;
run;

data want ;
  set have ;
%include code /source2 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 04 Apr 2017 16:31:29 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-04-04T16:31:29Z</dc:date>
    <item>
      <title>how to insert a macro variable value as a statement in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347067#M273263</link>
      <description>&lt;P&gt;I have a macro variable (&amp;amp;stmt1) whose value is&amp;nbsp;&lt;/P&gt;&lt;P&gt;varname = 'varvalue';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I put this macro variable's value in a data step to add this varname into the dataset?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;what I want to do is this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data dataset1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; set dataset1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; varname='varvalue'; /* this is done by insert the macro variable &amp;amp;stmt1 */&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 15:10:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347067#M273263</guid>
      <dc:creator>ksfang</dc:creator>
      <dc:date>2017-04-04T15:10:05Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert a macro variable value as a statement in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347079#M273264</link>
      <description>Please use the symget wth macro variable name. consider that the macro variable name is varvalue then i write &lt;BR /&gt;&lt;BR /&gt;varname=symget('varvalue');</description>
      <pubDate>Tue, 04 Apr 2017 15:30:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347079#M273264</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2017-04-04T15:30:23Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert a macro variable value as a statement in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347083#M273265</link>
      <description>&lt;P&gt;It might help to show your actual macro variable creation statement so we can tell which is your macro variable and which is the value of the variable. Too much "var" stuff hanging around.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is this what you are looking for?&lt;/P&gt;
&lt;PRE&gt;%let macrovar= variablename='abc';

data junk;
  &amp;amp;macrovar;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I recommend that you do not use the&lt;BR /&gt;Data dataset1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set dataset1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;until you have enough experience that you don't need to ask this simple of question. It is entirely too easy to destroy your data or cause problems that are difficult to diagnose with that code structure.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 15:36:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347083#M273265</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-04-04T15:36:45Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert a macro variable value as a statement in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347089#M273266</link>
      <description>&lt;P&gt;I would really advise against such a setup. &amp;nbsp;Why have the variable in the macro variable, that doesn't make sense, the point of macro is flexibility, your removing that. &amp;nbsp;Do:&lt;/P&gt;
&lt;PRE&gt;%let stmt1=varvalue;

data dataset1;
  set dataset1;
  varname="&amp;amp;stmt1.";
run;&lt;/PRE&gt;
&lt;P&gt;Far clearner and easier to maintain.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 15:49:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347089#M273266</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-04T15:49:09Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert a macro variable value as a statement in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347094#M273267</link>
      <description>&lt;P&gt;Hi RW9,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The issue is that I am reading in a spreadsheet, for the varname and varvalue. E.g.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Column A &amp;nbsp; &amp;nbsp; &amp;nbsp; Column B&lt;/P&gt;&lt;P&gt;var1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;eq 'abc'&lt;/P&gt;&lt;P&gt;var2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;eq 'xyz'&lt;/P&gt;&lt;P&gt;var3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;eq '123'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So in my macro I do not know the varname specifically, which needs to be read in from a spreadsheet.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 16:02:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347094#M273267</guid>
      <dc:creator>ksfang</dc:creator>
      <dc:date>2017-04-04T16:02:28Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert a macro variable value as a statement in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347106#M273268</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/97684"&gt;@ksfang&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi RW9,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your reply.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The issue is that I am reading in a spreadsheet, for the varname and varvalue. E.g.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Column A &amp;nbsp; &amp;nbsp; &amp;nbsp; Column B&lt;/P&gt;
&lt;P&gt;var1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;eq 'abc'&lt;/P&gt;
&lt;P&gt;var2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;eq 'xyz'&lt;/P&gt;
&lt;P&gt;var3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;eq '123'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in my macro I do not know the varname specifically, which needs to be read in from a spreadsheet.&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This certainly isn't the same problem that was mentioned at the start of this thread. There, you were using an equal sign and the statement you were trying to create was assigning values. Now, you have the operator eq, which can only be used in WHERE statement or an IF statement, you are not assigning values. So this is a very confusing explanation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Given that I'm not sure what you are actually trying to do, I&amp;nbsp;nevertheless think that perhaps the solution is something like this (and if there are further clarifications on the actual problem, I reserve the right to retract my solution). You should read the column A values into it's own macro variable (for example &amp;amp;colA) and read the column B values into its own macro variable (for example, &amp;amp;colB) and then in the proper place in the data step, you use&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;amp;colA &amp;amp;colB&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 16:25:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347106#M273268</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-04-04T16:25:59Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert a macro variable value as a statement in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347108#M273269</link>
      <description>Thank you! Let me try...</description>
      <pubDate>Tue, 04 Apr 2017 16:27:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347108#M273269</guid>
      <dc:creator>ksfang</dc:creator>
      <dc:date>2017-04-04T16:27:02Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert a macro variable value as a statement in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347109#M273270</link>
      <description>&lt;P&gt;If you want to use the value of a macro variable as a statement in a data step then just do that. &amp;nbsp;Macro variable resolution is just simple text substitution.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mvar=x2 = x**2 ;
data want;
  input x;
  &amp;amp;mvar ;
cards;
1
2
3
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But if you want to generate a lot of code from a data set then I would avoid macro variables and just write the code to file. &amp;nbsp;You can then use %INCLUDE to place it where you want it. &amp;nbsp;This has the advantages of avoiding macro variables and worries of macro quoting and also you can look at the file after it is generated and debug the code generating step more easily.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
   set my_metadata ;
   put target '=' expression ';' ;
run;

data want ;
  set have ;
%include code /source2 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Apr 2017 16:31:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347109#M273270</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-04-04T16:31:29Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert a macro variable value as a statement in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347128#M273271</link>
      <description>&lt;P&gt;Ah, ok, I see what you are doing. &amp;nbsp;I would really advise you don't go down that route. &amp;nbsp;I have seen many attempts to put programming in spreadsheets, and as Excel isn't good for data it&amp;nbsp;&lt;STRONG&gt;really&lt;/STRONG&gt; isn't good for data. &amp;nbsp;You will need to do all kinds of validation on it, otherwise you code will fall over each run. &amp;nbsp;Now, even if you still go with that method, even then I would advise to change the structure of the Excel file separating the items out, this will make your validation easier (and you can setup data validation in the Excel file also to reduce entry options), e.g:&lt;/P&gt;
&lt;P&gt;ColumnA &amp;nbsp;Operator &amp;nbsp;ColumnB&lt;/P&gt;
&lt;P&gt;var1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; eq &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; abc&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A second point, macro variable are not a good place for coding elements either. &amp;nbsp;Simpler to use the read in data to generate the code:&lt;/P&gt;
&lt;PRE&gt;data _null_;
  set imported_data end=last;
  if _n_= then call execute('data want; set have;');
  call execute(cat('if ',columna,' ',operator,' "',columnb,'" then flag="y";'));
  if last then call execute('run;');
run;&lt;/PRE&gt;
&lt;P&gt;This will generate a datastep with one if for each row in imported data, just and example.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 17:36:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347128#M273271</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-04T17:36:52Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert a macro variable value as a statement in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347129#M273272</link>
      <description>&lt;P&gt;Ah, ok, I see what you are doing. &amp;nbsp;I would really advise you don't go down that route. &amp;nbsp;I have seen many attempts to put programming in spreadsheets, and as Excel isn't good for data it&amp;nbsp;&lt;STRONG&gt;really&lt;/STRONG&gt; isn't good for data. &amp;nbsp;You will need to do all kinds of validation on it, otherwise you code will fall over each run. &amp;nbsp;Now, even if you still go with that method, even then I would advise to change the structure of the Excel file separating the items out, this will make your validation easier (and you can setup data validation in the Excel file also to reduce entry options), e.g:&lt;/P&gt;
&lt;P&gt;ColumnA &amp;nbsp;Operator &amp;nbsp;ColumnB&lt;/P&gt;
&lt;P&gt;var1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; eq &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; abc&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A second point, macro variable are not a good place for coding elements either. &amp;nbsp;Simpler to use the read in data to generate the code:&lt;/P&gt;
&lt;PRE&gt;data _null_;
  set imported_data end=last;
  if _n_= then call execute('data want; set have;');
  call execute(cat('if ',columna,' ',operator,' "',columnb,'" then flag="y";'));
  if last then call execute('run;');
run;&lt;/PRE&gt;
&lt;P&gt;This will generate a datastep with one if for each row in imported data, just and example.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 17:36:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347129#M273272</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-04T17:36:53Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert a macro variable value as a statement in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347145#M273273</link>
      <description>Thank you all for your suggestion.</description>
      <pubDate>Tue, 04 Apr 2017 18:43:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-insert-a-macro-variable-value-as-a-statement-in-data-step/m-p/347145#M273273</guid>
      <dc:creator>ksfang</dc:creator>
      <dc:date>2017-04-04T18:43:42Z</dc:date>
    </item>
  </channel>
</rss>

