<?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: Create Dynamic macro variable with Call Symput in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336953#M76449</link>
    <description>&lt;P&gt;Thanks Astounding ! It works ... Thanks a lot :smileyhappy&lt;/P&gt;</description>
    <pubDate>Wed, 01 Mar 2017 12:06:39 GMT</pubDate>
    <dc:creator>tjain90</dc:creator>
    <dc:date>2017-03-01T12:06:39Z</dc:date>
    <item>
      <title>Create Dynamic macro variable with Call Symput</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336912#M76432</link>
      <description>&lt;P&gt;Hi team,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to create dynamic macro variable with names in sequence and values as observation.&lt;/P&gt;&lt;P&gt;I have used below code but it is not working. Request you to please review:&lt;/P&gt;&lt;P&gt;data ab;&lt;BR /&gt;input id$;&lt;BR /&gt;cards;&lt;BR /&gt;A1&lt;BR /&gt;A2&lt;BR /&gt;A3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; rowToMacroVariable(dataSetName,variableName,macroVariableName);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;data &amp;amp;dataSetName;&lt;/P&gt;&lt;P&gt;set &amp;amp;dataSetName;&lt;/P&gt;&lt;P&gt;call symput(&amp;amp;macroVariableName||left(_n_),&amp;amp;variableName);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;%&lt;STRONG&gt;&lt;I&gt;rowToMacroVariable&lt;/I&gt;&lt;/STRONG&gt;(ab,id,id);&lt;/P&gt;&lt;P&gt;%&lt;STRONG&gt;&lt;I&gt;rowToMacroVariable&lt;/I&gt;&lt;/STRONG&gt;(ab,id,TJ);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now it should have created below macro variabls:&lt;/P&gt;&lt;P&gt;id1 A1&lt;/P&gt;&lt;P&gt;id2 A2&lt;/P&gt;&lt;P&gt;id3 A3&lt;/P&gt;&lt;P&gt;TJ1 A1&lt;/P&gt;&lt;P&gt;TJ2 A2&lt;/P&gt;&lt;P&gt;TJ3 A3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but it is not working &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 10:38:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336912#M76432</guid>
      <dc:creator>tjain90</dc:creator>
      <dc:date>2017-03-01T10:38:32Z</dc:date>
    </item>
    <item>
      <title>Re: Create Dynamic macro variable with Call Symput</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336919#M76436</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You forgot to enclose &amp;amp;macroVariableName with double quotes as the first argument to call symput is a string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro rowToMacroVariable(dataSetName,variableName,macroVariableName);
data &amp;amp;dataSetName;
	set &amp;amp;dataSetName;
	call symput("&amp;amp;macroVariableName"||left(_n_),&amp;amp;variableName);
run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Mar 2017 10:54:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336919#M76436</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2017-03-01T10:54:52Z</dc:date>
    </item>
    <item>
      <title>Re: Create Dynamic macro variable with Call Symput</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336923#M76437</link>
      <description>&lt;P&gt;Well, first off, as always, why. &amp;nbsp;Macro is not a replacement for Base SAS programming, it is just a find/replace mechanism. &amp;nbsp;In 99% of the cases I have seen people using macro it is because they don't know Base SAS well enough and try to patch it up, which ends up with messy, unmanigable code. &amp;nbsp;So the question is what are you trying to do with these macro variables - there are many methods of working with data, and even metadata which require no macro code (arrays, normalisation, variable lists, sashelp tables etc.) all designed to make your coding simple. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;The reason I mention the above is that from your code its clear you don't understand macro programming. &amp;nbsp;Let me start by suggesting you read up on Variable Scope. &amp;nbsp;Creating macros witihin the macro means that they are Local to the macro, and not available outside the macro. &amp;nbsp;Secondly your code needs a few tweaks:&lt;/P&gt;
&lt;PRE&gt;data _null_;&lt;BR /&gt; set ab;&lt;BR /&gt; call symput("id"||strip(put(_n_,best.)),id);&lt;BR /&gt;run;
&lt;/PRE&gt;
&lt;P&gt;This code will work, note you don't need to create a dataset (data xyz;) to do this, as the data already exists. &amp;nbsp;Secondly the you need to quote the macro variable to get it to resolve in the above i just put "id", but it would be "&amp;amp;Macrovariable"||...&lt;/P&gt;
&lt;P&gt;This will create a whole set of macro variables based on the data, and they will be Global to the area where you run it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However that is all by the by, this is really not the way to proceed.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 11:00:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336923#M76437</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-03-01T11:00:51Z</dc:date>
    </item>
    <item>
      <title>Re: Create Dynamic macro variable with Call Symput</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336924#M76438</link>
      <description>&lt;P&gt;Thanks gamotte,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But its not working &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 11:00:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336924#M76438</guid>
      <dc:creator>tjain90</dc:creator>
      <dc:date>2017-03-01T11:00:27Z</dc:date>
    </item>
    <item>
      <title>Re: Create Dynamic macro variable with Call Symput</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336934#M76441</link>
      <description>&lt;P&gt;Hi RW9,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for such a good explanation. I agree with _null_ even i have used this in programming but while posting it on community i have designed a seperate example. Even my code was working fine with this way but I am not able to make this &lt;STRONG&gt;id1 &lt;/STRONG&gt;variable as&lt;STRONG&gt; global&lt;/STRONG&gt;. So can you please let me know where i am missing ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now why I have used this in macro is&amp;nbsp;because in my scripts we have 10 separate datasets and I need to create seperate global macro variables that are already used in further programming. I know we can optimize these things but its an level 2 task so please understand somebody's situation before commenting on there knowledge skills or why they have used those thing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I agree that you must be having wide knowldge and I am happy to have people like you on SAS community because this is how we can learn and enhance our skills on SAS &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 11:37:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336934#M76441</guid>
      <dc:creator>tjain90</dc:creator>
      <dc:date>2017-03-01T11:37:52Z</dc:date>
    </item>
    <item>
      <title>Re: Create Dynamic macro variable with Call Symput</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336936#M76442</link>
      <description>&lt;P&gt;Since you create your macro variables in a macro, they are created &lt;U&gt;local&lt;/U&gt; to the macro and won't exist in the global table when the macro has finished.&lt;/P&gt;
&lt;P&gt;Why do you want to put such a rather simple operation in a macro?&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 11:44:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336936#M76442</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-03-01T11:44:06Z</dc:date>
    </item>
    <item>
      <title>Re: Create Dynamic macro variable with Call Symput</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336937#M76443</link>
      <description>&lt;P&gt;You have hilighted the issue yourself then "&lt;SPAN&gt;we have &lt;/SPAN&gt;&lt;SPAN&gt;10 separate datasets" - if your able to generalise your code, then these datasets will be similar if not the same, structurally. &amp;nbsp;Thus put those 10 datasets together on under the other, use indsname= to get filename if you need to, then process the data as one file with by groups.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;You can't define macros witihin a macro as they will be local, unless you explicitly set them as global beforehand - hence you would need to hard code either in the main program as %global, or define them in the main program as I gave earlier. &amp;nbsp;Even a change to the thinking would mitigate this, rather than have these in a macro, define them in the main program, and have your macro calls just do the function. &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 11:47:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336937#M76443</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-03-01T11:47:52Z</dc:date>
    </item>
    <item>
      <title>Re: Create Dynamic macro variable with Call Symput</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336938#M76444</link>
      <description>&lt;P&gt;If you need to keep lists of things from step to step, it's usually better to store them in datasets and use those to create dynamic code or formats or similar. It depends on what you need to do with those lists further on.&lt;/P&gt;
&lt;P&gt;Another way to store lists in macro variables is&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro rowToMacroVariable(dataSetName,variableName,macroVariableName);
%global &amp;amp;macrovariablename;
proc sql noprint;
select &amp;amp;variablename into :&amp;amp;macrovariablename separated by ','
from &amp;amp;datasetname
;
quit;
%mend;

%rowToMacroVariable(sashelp.class,name,test);

%put &amp;amp;test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you have one global macro variable that contains all values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 11:48:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336938#M76444</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-03-01T11:48:06Z</dc:date>
    </item>
    <item>
      <title>Re: Create Dynamic macro variable with Call Symput</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336941#M76447</link>
      <description>&lt;P&gt;And keep in mind that all those macro variables have to be kept in memory, and you can run out of RAM space. Or macro variables with lists can exceed the maximum size of macro variables. Datasets are virtually infinite in comparison.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 11:51:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336941#M76447</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-03-01T11:51:57Z</dc:date>
    </item>
    <item>
      <title>Re: Create Dynamic macro variable with Call Symput</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336950#M76448</link>
      <description>&lt;P&gt;If the only remaining issue is making these macro variables global, switch from CALL SYMPUT to CALL SYMPUTX. &amp;nbsp;It supports a third argument, that can simply be specified as "G" to make the macro variables global.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;call symputx("macro_variable_name", "macro_variable_value", "G");&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 12:02:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336950#M76448</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-03-01T12:02:58Z</dc:date>
    </item>
    <item>
      <title>Re: Create Dynamic macro variable with Call Symput</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336953#M76449</link>
      <description>&lt;P&gt;Thanks Astounding ! It works ... Thanks a lot :smileyhappy&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 12:06:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336953#M76449</guid>
      <dc:creator>tjain90</dc:creator>
      <dc:date>2017-03-01T12:06:39Z</dc:date>
    </item>
    <item>
      <title>Re: Create Dynamic macro variable with Call Symput</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336975#M76451</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Related but does not answer ops question

Defining a large number of global macro variables;

inspired by
https://goo.gl/YRzFGM
https://communities.sas.com/t5/Base-SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336912

HAVE
====

Up to 40 obs from sashelp.class total obs=19

Obs    NAME       SEX    AGE    HEIGHT    WEIGHT

  1    Alfred      M      14     69.0      112.5

  2    Alice       F      13     56.5       84.0  (turn into macro variables)

  3    Barbara     F      13     65.3       98.0
  4    Carol       F      14     62.8      102.5
  5    Henry       M      14     63.5      102.5
  6    James       M      12     57.3       83.0
  7    Jane        F      12     59.8       84.5
  8    Janet       F      15     62.5      112.5
  9    Jeffrey     M      13     62.5       84.0
 10    John        M      12     59.0       99.5
 11    Joyce       F      11     51.3       50.5
 12    Judy        F      14     64.3       90.0
 13    Louise      F      12     56.3       77.0
 14    Mary        F      15     66.5      112.0
 15    Philip      M      16     72.0      150.0
 16    Robert      M      12     64.8      128.0
 17    Ronald      M      15     67.0      133.0
 18    Thomas      M      11     57.5       85.0
 19    William     M      15     66.5      112.0


WANT macro variables NAME SEX AGE HEIGHT WEIGHT
===============================================

NAME        = Alice
SEX         = F
AGE         = 13
HEIGHT      = 56.5
WEIGHT      = 84

FULL SOLUTION
=============

%symdel name sex age height weight; * just in case you rerun;
%let dsid = %sysfunc(open(sashelp.class(where=(name='Alice')),i));
%syscall    set(dsid);
%let rc   =%sysfunc(fetchobs(&amp;amp;dsid,1));
%let rc   =%sysfunc(close(&amp;amp;dsid));


%put
   &amp;amp;=NAME
   &amp;amp;=SEX
   &amp;amp;=AGE
   &amp;amp;=HEIGHT
   &amp;amp;=WEIGHT
   ;

NAME        = Alice
SEX         = F
AGE         = 13
HEIGHT      = 56.5
WEIGHT      = 84

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Mar 2017 13:48:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Dynamic-macro-variable-with-Call-Symput/m-p/336975#M76451</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-03-01T13:48:24Z</dc:date>
    </item>
  </channel>
</rss>

