<?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 macro variables with list of vars for each pop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/create-macro-variables-with-list-of-vars-for-each-pop/m-p/875748#M346027</link>
    <description>Inside a macro definition, SQL creates %local macro variables.  But you need those macro variables to be global instead of local, so they still exist after the macro finishes executing.&lt;BR /&gt;&lt;BR /&gt;Add a statement after the %do statement and before the subsequent proc SQL statement:&lt;BR /&gt;&lt;BR /&gt;%global vector_vars&amp;amp;j;&lt;BR /&gt;&lt;BR /&gt;It appears to be the proper name that needs to persist once your macro finishes executing.</description>
    <pubDate>Mon, 15 May 2023 07:29:30 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2023-05-15T07:29:30Z</dc:date>
    <item>
      <title>create macro variables with list of vars for each pop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-macro-variables-with-list-of-vars-for-each-pop/m-p/875741#M346025</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have a data set that contain list of variables for each population.&lt;/P&gt;
&lt;P&gt;I want to perform a dynamic sas code that for each population will create list of variables in a macro variable.&lt;/P&gt;
&lt;P&gt;Each macro variable name should reflect the population .&lt;/P&gt;
&lt;P&gt;The macro variables will contain the vars values with comma between values.&lt;/P&gt;
&lt;P&gt;Expected results are :&lt;/P&gt;
&lt;P&gt;Vector_Vars1=X Y Z;&lt;BR /&gt;Vector_Vars1=X2 Y W;&lt;BR /&gt;Vector_Vars3=X Y R T Q;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unfortunately I get macro variables with no value&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;25         GOPTIONS ACCESSIBLE;
WARNING: Apparent symbolic reference VECTOR_VARS1 not resolved.
26         %put &amp;amp;Vector_Vars1;
&amp;amp;Vector_Vars1
27         %put &amp;amp;Vector_Vars2;
WARNING: Apparent symbolic reference VECTOR_VARS2 not resolved.
&amp;amp;Vector_Vars2
28         %put &amp;amp;Vector_Vars3;
WARNING: Apparent symbolic reference VECTOR_VARS3 not resolved.
&amp;amp;Vector_Vars3&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is the code I run&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data vars_list;
input population var_name $;
cards;
1 X
1 Y
1 Z
2 X2
2 Y
2 W
3 X
3 Y
3 R
3 T
3 Q
;
Run;


proc sql noprint;
select distinct population into : Vector_population SEPARATED by '+'
from vars_list
;
quit;
%put &amp;amp;Vector_population;
%let n_Vector_population = %sysfunc(countw(&amp;amp;Vector_population));
%put &amp;amp;n_Vector_population;


%macro RRR; 
%do j=1 %to &amp;amp;n_Vector_population.;
%let pop=%scan(&amp;amp;Vector_population.,&amp;amp;j.,+);
proc sql noprint;
select distinct VAR_name  into : Vector_Vars&amp;amp;pop. SEPARATED by '  '
from vars_list
where population=&amp;amp;pop.
;
quit;
%end;
%mend  RRR;
%RRR
%put &amp;amp;Vector_Vars1;
%put &amp;amp;Vector_Vars2;
%put &amp;amp;Vector_Vars3;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 May 2023 06:41:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-macro-variables-with-list-of-vars-for-each-pop/m-p/875741#M346025</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-05-15T06:41:07Z</dc:date>
    </item>
    <item>
      <title>Re: create macro variables with list of vars for each pop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-macro-variables-with-list-of-vars-for-each-pop/m-p/875748#M346027</link>
      <description>Inside a macro definition, SQL creates %local macro variables.  But you need those macro variables to be global instead of local, so they still exist after the macro finishes executing.&lt;BR /&gt;&lt;BR /&gt;Add a statement after the %do statement and before the subsequent proc SQL statement:&lt;BR /&gt;&lt;BR /&gt;%global vector_vars&amp;amp;j;&lt;BR /&gt;&lt;BR /&gt;It appears to be the proper name that needs to persist once your macro finishes executing.</description>
      <pubDate>Mon, 15 May 2023 07:29:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-macro-variables-with-list-of-vars-for-each-pop/m-p/875748#M346027</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-05-15T07:29:30Z</dc:date>
    </item>
    <item>
      <title>Re: create macro variables with list of vars for each pop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-macro-variables-with-list-of-vars-for-each-pop/m-p/875785#M346048</link>
      <description>&lt;P&gt;No macro needed, no looping needed, and now the macro variables created are global.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=vars_list out=vars_list_t;
	by population;
	var var_name;
run;
data vars_list_t1;
    length all_cols $ 200;
    set vars_list_t;
    all_cols=catx(' ',of col:);
    keep all_cols population;
run;
proc sql noprint;
	select all_cols into :vectorvars1- from vars_list_t1 order by population;
quit;
%put &amp;amp;=vectorvars1;
%put &amp;amp;=vectorvars2;
%put &amp;amp;=vectorvars3;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 May 2023 13:05:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-macro-variables-with-list-of-vars-for-each-pop/m-p/875785#M346048</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-05-15T13:05:43Z</dc:date>
    </item>
    <item>
      <title>Re: create macro variables with list of vars for each pop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-macro-variables-with-list-of-vars-for-each-pop/m-p/875798#M346057</link>
      <description>&lt;P&gt;You could also do this using CALL SYMPUTX in a DATA step with BY-GROUP processing.&amp;nbsp; e.g.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_ ;
  set vars_list ;
  by population ;&lt;BR /&gt;  length varlist $200;
  retain varlist ;
  if first.population then varlist=var_name ;
  else varlist=cats(" ",varlist,var_name) ;
  if last.population then call symputx(cats("Vector_Vars",population),varlist) ;
run ;
%put _user_ ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or fewer statements, but less readable to my eye, below avoids creating the ancillary variable VARLIST by executing CALL SYMPUTX once per record and using symget to resolve the macro variable during execution-time:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_ ;
  set vars_list ;
  by population ;
  if first.population then call symputx(cats("Vector_Vars",population),var_name) ;
  else call symputx(cats("Vector_Vars",population)
                   ,catx(" ",symget(cats("Vector_Vars",population)),var_name)
                    );
run ;
%put _user_ ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 May 2023 12:49:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-macro-variables-with-list-of-vars-for-each-pop/m-p/875798#M346057</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-05-15T12:49:05Z</dc:date>
    </item>
    <item>
      <title>Re: create macro variables with list of vars for each pop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-macro-variables-with-list-of-vars-for-each-pop/m-p/875847#M346072</link>
      <description>&lt;P&gt;Or just use CALL EXECUTE() to generate a series of %LET statements.&lt;/P&gt;
&lt;PRE&gt;62   data _null_ ;
63     set vars_list ;
64     by population ;
65     mvar=cats("Vector_Vars",population);
66     if first.population then call execute(catx(' ','%let',mvar,'=;'));
67     call execute(catx(' ','%let',mvar,cats('=&amp;amp;',mvar),var_name,';'));
68   run ;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds

NOTE: There were 11 observations read from the data set WORK.VARS_LIST.

NOTE: CALL EXECUTE routine executed successfully, but no SAS statements were generated.
69   %put _user_ ;
GLOBAL VECTOR_VARS1 X Y Z
GLOBAL VECTOR_VARS2 X2 Y W
GLOBAL VECTOR_VARS3 X Y R T Q
&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 May 2023 16:29:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-macro-variables-with-list-of-vars-for-each-pop/m-p/875847#M346072</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-15T16:29:27Z</dc:date>
    </item>
    <item>
      <title>Re: create macro variables with list of vars for each pop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-macro-variables-with-list-of-vars-for-each-pop/m-p/875852#M346074</link>
      <description>&lt;P&gt;You didn't create a global macro variable, only local. Add a GLOBAL statement to have it work.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That being said, a data step is more efficient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql noprint;
select distinct population into : Vector_population SEPARATED by '+'
from vars_list
;
quit;
%put &amp;amp;Vector_population;
%let n_Vector_population = %sysfunc(countw(&amp;amp;Vector_population));
%put &amp;amp;n_Vector_population;




%macro RRR; 
%do j=1 %to &amp;amp;n_Vector_population.;
%let pop=%scan(&amp;amp;Vector_population.,&amp;amp;j.,+);
&lt;FONT size="4" color="#FF0000"&gt;&lt;STRONG&gt;%global Vector_vars&amp;amp;pop.;&lt;/STRONG&gt;&lt;/FONT&gt;

proc sql noprint;
select distinct VAR_name  into : Vector_Vars&amp;amp;pop. SEPARATED by '  '
from vars_list
where population=&amp;amp;pop.
;
quit;
%end;
%mend  RRR;
%RRR
%put &amp;amp;Vector_Vars1;
%put &amp;amp;Vector_Vars2;
%put &amp;amp;Vector_Vars3;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 May 2023 17:13:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-macro-variables-with-list-of-vars-for-each-pop/m-p/875852#M346074</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-05-15T17:13:36Z</dc:date>
    </item>
    <item>
      <title>Re: create macro variables with list of vars for each pop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-macro-variables-with-list-of-vars-for-each-pop/m-p/875916#M346088</link>
      <description>&lt;P&gt;It is great but in the real world I need to concatenate 100 variables and I get warning&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;WARNING: In a call to the CATX function, the buffer allocated for the result was not long enough to contain the concatenation of 
         all the arguments. The correct result would contain 1326 characters, but the actual result might either be truncated to 
         1000 character(s) or be completely blank, depending on the calling environment. The following note indicates the left-most 
         argument that caused truncation.&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 May 2023 04:56:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-macro-variables-with-list-of-vars-for-each-pop/m-p/875916#M346088</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-05-16T04:56:38Z</dc:date>
    </item>
    <item>
      <title>Re: create macro variables with list of vars for each pop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-macro-variables-with-list-of-vars-for-each-pop/m-p/875922#M346090</link>
      <description>&lt;P&gt;Which length did you use in the LENGTH statement?&lt;/P&gt;</description>
      <pubDate>Tue, 16 May 2023 06:17:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-macro-variables-with-list-of-vars-for-each-pop/m-p/875922#M346090</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-05-16T06:17:08Z</dc:date>
    </item>
  </channel>
</rss>

