<?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 create a macro using SYMPUTX if the name of the macro you want to create contains a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-macro-using-SYMPUTX-if-the-name-of-the-macro-you/m-p/813331#M320977</link>
    <description>&lt;P&gt;Alternative solution: use dictionaries - Here is an example which only create the macro variable is there is at least one group variable. You can extend it with all the cases with a merge...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
    group_A=1;
    group_B=1;
    tmp=1;
run;

data two;
    group=1;
run;

data three;
    tmp=1;
run;


proc sql;
    create table ref as
    select distinct memname, count(*) as cnt_group
    from dictionary.columns
    where upcase(libname)='WORK' and
          upcase(name) like 'GROUP%'
    group by memname;
quit;


data _null_;
    set ref;
    call symputx (cats(memname,_n_),cnt_group);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 14 May 2022 06:57:39 GMT</pubDate>
    <dc:creator>xxformat_com</dc:creator>
    <dc:date>2022-05-14T06:57:39Z</dc:date>
    <item>
      <title>How to create a macro using SYMPUTX if the name of the macro you want to create contains a macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-macro-using-SYMPUTX-if-the-name-of-the-macro-you/m-p/813173#M320880</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to create macro variables to identify how many variables start with "GROUP" in a number of datasets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For each dataset I want a macro variable called Tree_Group&amp;amp;i. where &amp;amp;i. relates to the dataset. Does anyone know how to achieve this? I know you cant use a macro variable in symputx when creating a new macro so unsure how to proceed as the below wont work!&lt;CODE class=" language-sas"&gt;
&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro Test(i);
proc contents
     data = Test_&amp;amp;i.
          out = data_info&amp;amp;i.(where=(upcase(substr(name,1,5)="GROUP"))
               keep = name);
run;

data _NULL_;
set data_info&amp;amp;i. nobs=GROUP_NUMBER;
call symputx(Tree_Group&amp;amp;i,GROUP_NUMBER);
run;

%mend;
%Test(1);
%Test(2);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 May 2022 10:58:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-macro-using-SYMPUTX-if-the-name-of-the-macro-you/m-p/813173#M320880</guid>
      <dc:creator>EC27556</dc:creator>
      <dc:date>2022-05-13T10:58:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a macro using SYMPUTX if the name of the macro you want to create contains a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-macro-using-SYMPUTX-if-the-name-of-the-macro-you/m-p/813176#M320881</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;call symputx("Tree_Group&amp;amp;i",GROUP_NUMBER);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the data sets have the same name except for consecutive numbering at the end of the data set name, there's really no need to do this via macros. PROC FREQ working on SASHELP.VCOLUMN ought to produce this without macros and without loops.&lt;/P&gt;</description>
      <pubDate>Fri, 13 May 2022 11:06:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-macro-using-SYMPUTX-if-the-name-of-the-macro-you/m-p/813176#M320881</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-05-13T11:06:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a macro using SYMPUTX if the name of the macro you want to create contains a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-macro-using-SYMPUTX-if-the-name-of-the-macro-you/m-p/813221#M320897</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;I know you cant use a macro variable in symputx when creating a new macro so unsure how to proceed as the below wont work!&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is just a flat out false statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This line in your code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;call symputx(Tree_Group&amp;amp;i,GROUP_NUMBER);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is looking for a variable named TREE_GROUP1 or TREE_GROUP2 that contains the name of the macro variable you want to create.&lt;/P&gt;
&lt;P&gt;If instead you want it to create a macro variable named TREE_GROUP1 or TREE_GROUP2 then you need to pass that STRING to the function call.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;call symputx("Tree_Group&amp;amp;i",GROUP_NUMBER);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 May 2022 13:45:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-macro-using-SYMPUTX-if-the-name-of-the-macro-you/m-p/813221#M320897</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-13T13:45:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a macro using SYMPUTX if the name of the macro you want to create contains a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-macro-using-SYMPUTX-if-the-name-of-the-macro-you/m-p/813227#M320901</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note there are some other issues in your macro as well.&amp;nbsp; There is a parentheses problem, and you will also need to add the "G" parameter to CALL SYMPUTX assuming your goal is to make a global macro variable:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro Test(i);
proc contents
     data = Test_&amp;amp;i.
          out = data_info&amp;amp;i.(where=(upcase(substr(name,1,5))="GROUP")  /*fixed parentheses*/
               keep = name);
run;

data _NULL_;
set data_info&amp;amp;i. nobs=GROUP_NUMBER;
call symputx("Tree_Group&amp;amp;i",GROUP_NUMBER,"G");    /*added quotes to first argument, and third argument G to make global macro var*/
stop ;  /*you only need to read one record*/ 
run;

%mend;

data test_1  ;
  array foo{*} Group1 Grouper NotGroup ;
run ;

%Test(1)

%put &amp;amp;Tree_group1 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note if you're going to build a macro, and are interested in macro programming, you might think about options for making this a more general macro, e.g. below macro uses your core approach, but allows user to input the dataset name, variable name prefix, and name of the global macro variable to be created:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro CountVars
  (data=
  ,prefix=
  ,macvar=
   );

proc contents 
  data = &amp;amp;data (keep=&amp;amp;prefix: )
  out = __MyVars(keep = name) 
  noprint
;
run;

data _NULL_;
  set __MyVars nobs=GROUP_NUMBER;
  call symputx("&amp;amp;macvar",GROUP_NUMBER,"G");   
  stop ;  
run;

proc delete data=__MyVars ;
run ;

%mend;

%CountVars
  (data=Test_1
  ,prefix=Group
  ,macvar=GroupCount1
   )

%put &amp;amp;groupcount1 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another macro solution would be to write a function-style macro %VarList which allows a user to input a dataset and will return a list of the variables found.&amp;nbsp; Such macros exist in user group papers in lexjansen.com, they are intermediate / advanced macro coding.&amp;nbsp; If you have the macro, and then you could find the count with just:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put There are %sysfunc(countw(%varlist(mydata(keep=Group: )))) variables in work.mydata that have a variable name starting with GROUP ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There is complexity to writing a VARLIST macro that allows you to specify keep/drop options in the usual SAS way, but once you have such a function-style macro, it becomes handy in many situations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 May 2022 14:02:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-macro-using-SYMPUTX-if-the-name-of-the-macro-you/m-p/813227#M320901</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-05-13T14:02:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a macro using SYMPUTX if the name of the macro you want to create contains a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-macro-using-SYMPUTX-if-the-name-of-the-macro-you/m-p/813331#M320977</link>
      <description>&lt;P&gt;Alternative solution: use dictionaries - Here is an example which only create the macro variable is there is at least one group variable. You can extend it with all the cases with a merge...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
    group_A=1;
    group_B=1;
    tmp=1;
run;

data two;
    group=1;
run;

data three;
    tmp=1;
run;


proc sql;
    create table ref as
    select distinct memname, count(*) as cnt_group
    from dictionary.columns
    where upcase(libname)='WORK' and
          upcase(name) like 'GROUP%'
    group by memname;
quit;


data _null_;
    set ref;
    call symputx (cats(memname,_n_),cnt_group);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 14 May 2022 06:57:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-macro-using-SYMPUTX-if-the-name-of-the-macro-you/m-p/813331#M320977</guid>
      <dc:creator>xxformat_com</dc:creator>
      <dc:date>2022-05-14T06:57:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a macro using SYMPUTX if the name of the macro you want to create contains a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-macro-using-SYMPUTX-if-the-name-of-the-macro-you/m-p/813352#M320995</link>
      <description>&lt;P&gt;Why?&lt;/P&gt;
&lt;P&gt;What is the purpose of stuffing so much information into macro variables?&lt;/P&gt;
&lt;P&gt;What are you going to do with the macro variables?&lt;/P&gt;
&lt;P&gt;If you want a list of the variables for a given dataset why not put the list into one macro variable?&lt;/P&gt;</description>
      <pubDate>Sat, 14 May 2022 16:39:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-macro-using-SYMPUTX-if-the-name-of-the-macro-you/m-p/813352#M320995</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-14T16:39:14Z</dc:date>
    </item>
  </channel>
</rss>

