<?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 can i define a macro variable by using another macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-can-i-define-a-macro-variable-by-using-another-macro/m-p/894852#M353482</link>
    <description>&lt;P&gt;I don't know where the typo(s) may be. Your data set shows variable seq_num and cblock. You have this "(select nblock from work.datahave" which from the shown data does NOT have any "nblock".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming since you used "nblock" more time that you mean nblock instead of Cblock as the macro variable name:&lt;/P&gt;
&lt;PRE&gt;data datahave;
 input seq_num $ cblock $;
datalines;
1 c1,c2
2 c3,c4
; run;&lt;BR /&gt;&lt;BR /&gt;data _null_;&lt;BR /&gt;   set datahave;&lt;BR /&gt;   call symputx(catx('_','nblock',seq_num),cblock);&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;No macro needed to create sequentially numbered macro variables.&lt;/P&gt;
&lt;P&gt;Don't even need the seq_num variable unless you need values other than actual sequence. This uses the data step increment counter to number macro variables sequentially in order of appearance in a data set.&lt;/P&gt;
&lt;PRE&gt;data _null_;
   set datahave;
    call symputx(catx('_','nblock',_n_),cblock);
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 18 Sep 2023 21:01:39 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-09-18T21:01:39Z</dc:date>
    <item>
      <title>how can i define a macro variable by using another macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-can-i-define-a-macro-variable-by-using-another-macro/m-p/894850#M353481</link>
      <description>&lt;P&gt;Hi, i would like to define many macro variables by using "reference table (datahave) namely&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;amp;nblock1 , &amp;amp;nblock_2 . could anyone pls solve or give an idea ? thank you in advance. Best Regards.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data datahave;&lt;BR /&gt;input seq_num $ cblock $;&lt;BR /&gt;datalines;&lt;BR /&gt;1 c1,c2&lt;BR /&gt;2 c3,c4&lt;BR /&gt;;&amp;nbsp;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;options symbolgen;&lt;/P&gt;&lt;P&gt;%let start = 1 ;&lt;BR /&gt;%let end = 2 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro macvar (start,end);&lt;BR /&gt;%do seq_num = &amp;amp;start. %to &amp;amp;end. ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let nblock_&amp;amp;seq_num. =&lt;BR /&gt;(select nblock from work.datahave where seq_num = &amp;amp;sec_num.);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%end;&lt;BR /&gt;%mend ;&lt;BR /&gt;%macvar(start=&amp;amp;start , end=&amp;amp;end)&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;note:&amp;nbsp;&lt;/P&gt;&lt;P&gt;if i define macro one by one ( i need more than 30) it works.&lt;/P&gt;&lt;P&gt;%let nblock_1 =&amp;nbsp;&amp;nbsp;(select nblock from work.datahave where seq_num = &amp;amp;sec_num.);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Sep 2023 20:49:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-can-i-define-a-macro-variable-by-using-another-macro/m-p/894850#M353481</guid>
      <dc:creator>alparle</dc:creator>
      <dc:date>2023-09-18T20:49:18Z</dc:date>
    </item>
    <item>
      <title>Re: how can i define a macro variable by using another macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-can-i-define-a-macro-variable-by-using-another-macro/m-p/894852#M353482</link>
      <description>&lt;P&gt;I don't know where the typo(s) may be. Your data set shows variable seq_num and cblock. You have this "(select nblock from work.datahave" which from the shown data does NOT have any "nblock".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming since you used "nblock" more time that you mean nblock instead of Cblock as the macro variable name:&lt;/P&gt;
&lt;PRE&gt;data datahave;
 input seq_num $ cblock $;
datalines;
1 c1,c2
2 c3,c4
; run;&lt;BR /&gt;&lt;BR /&gt;data _null_;&lt;BR /&gt;   set datahave;&lt;BR /&gt;   call symputx(catx('_','nblock',seq_num),cblock);&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;No macro needed to create sequentially numbered macro variables.&lt;/P&gt;
&lt;P&gt;Don't even need the seq_num variable unless you need values other than actual sequence. This uses the data step increment counter to number macro variables sequentially in order of appearance in a data set.&lt;/P&gt;
&lt;PRE&gt;data _null_;
   set datahave;
    call symputx(catx('_','nblock',_n_),cblock);
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Sep 2023 21:01:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-can-i-define-a-macro-variable-by-using-another-macro/m-p/894852#M353482</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-09-18T21:01:39Z</dc:date>
    </item>
    <item>
      <title>Re: how can i define a macro variable by using another macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-can-i-define-a-macro-variable-by-using-another-macro/m-p/894861#M353483</link>
      <description>&lt;P&gt;I think you have a typo sec_num where you intend seq_num.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I change that, the code "works" (i.e. runs without errors) but is probably not doing what you intend. The macro variables NBlock_1 and NBlock_2 will have the text for a SQL query.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datahave;
input seq_num $ cblock $;
datalines;
1 c1,c2
2 c3,c4
;
run;

%let start = 1 ;
%let end = 2 ;

%macro macvar (start,end);
%do seq_num = &amp;amp;start. %to &amp;amp;end. ;
  %let nblock_&amp;amp;seq_num. =
  (select nblock from work.datahave where seq_num = &amp;amp;seq_num.);
%end;

%put _local_ ;
%mend ;

%macvar(start=&amp;amp;start , end=&amp;amp;end)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log shows:&lt;/P&gt;
&lt;PRE&gt;255  %macvar(start=&amp;amp;start , end=&amp;amp;end)
MACVAR END 2
MACVAR NBLOCK_1 (select nblock from work.datahave where seq_num = 1)
MACVAR NBLOCK_2 (select nblock from work.datahave where seq_num = 2)
MACVAR SEQ_NUM 3
MACVAR START 1
&lt;/PRE&gt;
&lt;P&gt;Can you back up and describe the big picture? Are you trying to make two macro variables NBlock_1 and NBlock_2 from datahave, because it has two records?&amp;nbsp; And what do you want the value of those macro variables to be?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Sep 2023 21:58:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-can-i-define-a-macro-variable-by-using-another-macro/m-p/894861#M353483</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-09-18T21:58:22Z</dc:date>
    </item>
    <item>
      <title>Re: how can i define a macro variable by using another macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-can-i-define-a-macro-variable-by-using-another-macro/m-p/894869#M353485</link>
      <description>&lt;P&gt;Do you want NBLOCK_1 to have the string&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;(select nblock from work.datahave where seq_num = 1)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or do you want NBLOCK_1 to have the value of NBLOCK from the dataset DATAHAVE?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select nblock
  into :NBLOCK_&amp;amp;seq_num
  from datahave
  where seq_num = &amp;amp;seq_num
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the later will there just be one observation with SEQ_NUM=1 ?&amp;nbsp; Or will there be multiple?&amp;nbsp; If multiple do you want to concatenate the values so you store them into one macro variable?&amp;nbsp; If so then use the SEPARATED BY option.&amp;nbsp; For example if you wanted them separated by a space then use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  into :NBLOCK_&amp;amp;seq_num separated by ' '&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Do you need the macro variables to exist after the macro finishes running?&amp;nbsp; Then make sure to add a %GLOBAL statement.&amp;nbsp; Or better only add the %GLOBAL statement when needed.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if not %symexist(NBLOCK_&amp;amp;seq_num) then %global NBLOCK_&amp;amp;seq_num;
select nblock
  into :NBLOCK_&amp;amp;seq_num
  from datahave
  where seq_num = &amp;amp;seq_num
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But then why not just use a DATA step?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set datahave;
  where seq_num between &amp;amp;start and &amp;amp;end ;
  call symputx(cats('NBLOCK_',seq_num),nblock);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you need them forced into the GLOBAL macro scope then add the optional third argument.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  call symputx(cats('NBLOCK_',seq_num),nblock,'g');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The real questions though are&amp;nbsp;&lt;STRONG&gt;What are you going to DO with a whole series of macro variables?&lt;/STRONG&gt;&amp;nbsp;And &lt;STRONG&gt;Why can't you just use the dataset instead?&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Sep 2023 05:26:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-can-i-define-a-macro-variable-by-using-another-macro/m-p/894869#M353485</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-19T05:26:06Z</dc:date>
    </item>
    <item>
      <title>Re: how can i define a macro variable by using another macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-can-i-define-a-macro-variable-by-using-another-macro/m-p/894884#M353491</link>
      <description>&lt;P&gt;What are you trying to achieve in the end?&lt;/P&gt;</description>
      <pubDate>Tue, 19 Sep 2023 06:10:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-can-i-define-a-macro-variable-by-using-another-macro/m-p/894884#M353491</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2023-09-19T06:10:37Z</dc:date>
    </item>
  </channel>
</rss>

