<?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: Creating and calling macro variables from a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590582#M169048</link>
    <description>How will you handle the situation to tweak the program if the example&lt;BR /&gt;records which I gave is in different program?&lt;BR /&gt;</description>
    <pubDate>Sat, 21 Sep 2019 11:22:32 GMT</pubDate>
    <dc:creator>Babloo</dc:creator>
    <dc:date>2019-09-21T11:22:32Z</dc:date>
    <item>
      <title>Creating and calling macro variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590576#M169044</link>
      <description>In my project we've decided to replace the hard coded values with the macro variables. Plan is to create the dataset with the list of hard corded values as one variable and the meaningful name for those values in another variable.&lt;BR /&gt;&lt;BR /&gt;E.g.&lt;BR /&gt;&lt;BR /&gt;Variables     Values&lt;BR /&gt;&lt;BR /&gt;Sex               'M'&lt;BR /&gt;Key_figure   '22365'&lt;BR /&gt;Id                   '659'&lt;BR /&gt;&lt;BR /&gt;And we would like to substitute the hard coded  values in our program with &amp;amp;sex, &amp;amp;key_figure....&lt;BR /&gt;&lt;BR /&gt;Appreciate if some one of you guide me by explaining how this approach works and how it can be implemented.&lt;BR /&gt;</description>
      <pubDate>Sat, 21 Sep 2019 08:54:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590576#M169044</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-09-21T08:54:24Z</dc:date>
    </item>
    <item>
      <title>Re: Creating and calling macro variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590577#M169045</link>
      <description>&lt;P&gt;I assume that what you have is something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data macro_vars;
   infile datalines;
   input varname $ value $;
datalines;
sex  m
key_figure  22365
id  659
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and you are looking for the next step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;&lt;BR /&gt; set have;
  call symput(varname, strip(value));
run;

%put &amp;amp;sex &amp;amp;key_figure &amp;amp;id;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 21 Sep 2019 10:26:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590577#M169045</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-09-21T10:26:34Z</dc:date>
    </item>
    <item>
      <title>Re: Creating and calling macro variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590578#M169046</link>
      <description>&lt;P&gt;Use&amp;nbsp;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n1nexcs36ctqk5n11uao7k9myz7y.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;call symputx()&lt;/A&gt;&amp;nbsp; to create macro variables in a data step.&lt;/P&gt;</description>
      <pubDate>Sat, 21 Sep 2019 09:07:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590578#M169046</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-09-21T09:07:26Z</dc:date>
    </item>
    <item>
      <title>Re: Creating and calling macro variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590582#M169048</link>
      <description>How will you handle the situation to tweak the program if the example&lt;BR /&gt;records which I gave is in different program?&lt;BR /&gt;</description>
      <pubDate>Sat, 21 Sep 2019 11:22:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590582#M169048</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-09-21T11:22:32Z</dc:date>
    </item>
    <item>
      <title>Re: Creating and calling macro variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590586#M169049</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;How will you handle the situation to tweak the program if the example&lt;BR /&gt;records which I gave is in different program?&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;As long as the dataset is in a permanent library, you just read it in the program where you need the macro variables.&lt;/P&gt;</description>
      <pubDate>Sat, 21 Sep 2019 11:51:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590586#M169049</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-09-21T11:51:17Z</dc:date>
    </item>
    <item>
      <title>Re: Creating and calling macro variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590598#M169057</link>
      <description>So if the program has ' where sex='M' ' then I can write it as ' where sex=&amp;amp;macrovariable '?</description>
      <pubDate>Sat, 21 Sep 2019 13:06:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590598#M169057</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-09-21T13:06:12Z</dc:date>
    </item>
    <item>
      <title>Re: Creating and calling macro variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590601#M169060</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;How will you handle the situation to tweak the program if the example&lt;BR /&gt;records which I gave is in different program?&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Easy to be done, depending on the format of your macro variables: sas dataset or external file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you need to change your current code with hard coded variables into a new code&lt;/P&gt;
&lt;P&gt;using the macro variables ? That also is programmable, depending on how long is your code.&lt;/P&gt;
&lt;P&gt;If it short - better do manually.&lt;/P&gt;</description>
      <pubDate>Sat, 21 Sep 2019 13:09:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590601#M169060</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-09-21T13:09:07Z</dc:date>
    </item>
    <item>
      <title>Re: Creating and calling macro variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590602#M169061</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;So if the program has ' where sex='M' ' then I can write it as ' where sex=&amp;amp;macrovariable '?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That's right.&lt;/P&gt;</description>
      <pubDate>Sat, 21 Sep 2019 13:10:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590602#M169061</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-09-21T13:10:09Z</dc:date>
    </item>
    <item>
      <title>Re: Creating and calling macro variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590606#M169065</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;So if the program has ' where sex='M' ' then I can write it as ' where sex=&amp;amp;macrovariable '?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes,&lt;STRONG&gt; but&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;Since SEX is a character variable in the SAS code you had to add quotes around the letter M so that the compiler knew you meant the string with the value M and not a variable named M.&amp;nbsp; So if you use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;sex=&amp;amp;macrovariable&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then value of macrovariable needs be&lt;/P&gt;
&lt;PRE&gt;"M"&lt;/PRE&gt;
&lt;P&gt;and not just&lt;/P&gt;
&lt;PRE&gt;M&lt;/PRE&gt;
&lt;P&gt;But having those quotes in the value of macro variable will then make it hard to also use that M as part of a word in the generated SAS code. For example as part of a dataset name.&amp;nbsp; With the quotes this wouldn't make sense.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data out&amp;amp;macrovariable ;
  set sashelp.class;
  where sex=&amp;amp;macrovariable;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might consider adding the quotes into the code where the code needs them instead of adding them to the value of the macro variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data out&amp;amp;macrovariable ;
  set sashelp.class;
  where sex="&amp;amp;macrovariable";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Sep 2019 14:05:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590606#M169065</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-09-21T14:05:09Z</dc:date>
    </item>
    <item>
      <title>Re: Creating and calling macro variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590608#M169067</link>
      <description>Answer to your questions:&lt;BR /&gt;&lt;BR /&gt;a. SAS dataset&lt;BR /&gt;b. Yes, have to replace the hard coded values with macro variables in a&lt;BR /&gt;programs&lt;BR /&gt;</description>
      <pubDate>Sat, 21 Sep 2019 13:32:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590608#M169067</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-09-21T13:32:32Z</dc:date>
    </item>
    <item>
      <title>Re: Creating and calling macro variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590611#M169069</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Building on &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; posts, here's an example that uses slightly changed variable names and then uses the macro variables in a WHERE statement -- just to show usage:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data macro_vars;
   length varname $10 value $15;
   infile datalines;
   input varname $ value $;
   call symputx(varname, left(value),'G');
datalines;
sex  F
key_figure  Alice
height  65.9
;
run;

%put &amp;amp;=sex &amp;amp;=key_figure &amp;amp;=65.9;
 
proc print data=sashelp.class;
  where (sex="&amp;amp;sex" and name="&amp;amp;key_figure") 
         or
	height gt &amp;amp;height;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Produces this output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="show_where_macro.png" style="width: 320px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/32652iA71CBBB421F96547/image-size/large?v=v2&amp;amp;px=999" role="button" title="show_where_macro.png" alt="show_where_macro.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Sat, 21 Sep 2019 13:45:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590611#M169069</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2019-09-21T13:45:27Z</dc:date>
    </item>
    <item>
      <title>Re: Creating and calling macro variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590624#M169077</link>
      <description>Now if I have to call the values from SQL table instead of SAS dataset, do&lt;BR /&gt;I need to change any thing in your proposed code?&lt;BR /&gt;&lt;BR /&gt;I will create a library for the SQL table.&lt;BR /&gt;</description>
      <pubDate>Sat, 21 Sep 2019 15:52:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590624#M169077</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-09-21T15:52:32Z</dc:date>
    </item>
    <item>
      <title>Re: Creating and calling macro variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590634#M169082</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Now if I have to call the values from SQL table instead of SAS dataset, do&lt;BR /&gt;I need to change any thing in your proposed code?&lt;BR /&gt;&lt;BR /&gt;I will create a library for the SQL table.&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;A dataset is a dataset, no matter how you use it (data step, procedure step, proc SQL) or where it resides (native SAS dataset file, remote DBMS, inside a library defined with Excel engine, ...).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You use the same data _null_ step with call symputx().&lt;/P&gt;</description>
      <pubDate>Sat, 21 Sep 2019 17:57:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-and-calling-macro-variables-from-a-dataset/m-p/590634#M169082</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-09-21T17:57:36Z</dc:date>
    </item>
  </channel>
</rss>

