<?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: Passing a Dynamic Variable from Data set as a Macro Parameter in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Passing-a-Dynamic-Variable-from-Data-set-as-a-Macro-Parameter/m-p/485710#M71884</link>
    <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The reason your macro doesn't work is because the macro is compiled before the data step is processed, i.e. the countw(&amp;amp;temp) will translate into countw(Variable_source) = 1 (same with the %scan()-function).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is one way of doing it without using a macro:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Input;                                                                         
input Variable_source $15.;                          
datalines;                                                                          
01 03
01 03 04
01 05
01 02
;

/* Looping through each dynamic array (using scan() to loop through the "words" and outputing each source as a new row) */
/* Creating dummy variable transpose_var=1 for transpose below and temporary variable rownumber to avoid sorting issues when transposing */
data work.input_spliced(drop=i);
	set work.input;
	rownumber = _n_;
	transpose_var = 1;
	do i = 1 to countw(Variable_source);
		Var_source_spliced = scan(Variable_source,i);
		output;
	end;
run;

/* Transposing the spliced table with variable prefix VARIABLE_ */
proc transpose data=work.input_spliced out=work.output(drop=rownumber _name_) prefix=VARIABLE_;
	by rownumber Variable_source;
	id Var_source_spliced;
	var transpose_var;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kind regards&lt;/P&gt;&lt;P&gt;/Calle&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit: Realized there was some unnecessary code in there, so I trimmed the solution a bit. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 10 Aug 2018 12:50:41 GMT</pubDate>
    <dc:creator>CalleJ</dc:creator>
    <dc:date>2018-08-10T12:50:41Z</dc:date>
    <item>
      <title>Passing a Dynamic Variable from Data set as a Macro Parameter</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Passing-a-Dynamic-Variable-from-Data-set-as-a-Macro-Parameter/m-p/485622#M71873</link>
      <description>&lt;P class="s570a4-10 iEJDri"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="s570a4-10 iEJDri"&gt;Hello everyone, I'm newish to SAS, so please bare with me. I'm trying to create multiple flag variables based on the contents of a parsed out variable. My macro isn't doing what I want it to do... it's parameter is passing as a static string, rather than a dynamic variable. Any help would be greatly appreciated.&lt;/P&gt;&lt;P class="s570a4-10 iEJDri"&gt;-----------&lt;/P&gt;&lt;P class="s570a4-10 iEJDri"&gt;Details!&lt;/P&gt;&lt;P class="s570a4-10 iEJDri"&gt;This is my input data:&lt;/P&gt;&lt;PRE class="s570a4-8 jsZgjh"&gt;&lt;CODE class="s570a4-7 bjpfNu"&gt;data Input;                                                                         
input Variable_source $15.;                          
datalines;                                                                          
01 03
01 03 04
01 05
01 02
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P class="s570a4-10 iEJDri"&gt;Here is my Macro code&lt;/P&gt;&lt;PRE class="s570a4-8 jsZgjh"&gt;&lt;CODE class="s570a4-7 bjpfNu"&gt;%macro parse (temp=);
	data output; set Input; 
		%local i;
		%do i = 1 %to %sysfunc(countw(&amp;amp;temp));
			%let code = %scan(&amp;amp;temp, &amp;amp;i, %str( ));
			VARIABLE_&amp;amp;code=1;
		%end;
	run;
%mend;

%parse (temp=Variable_source);&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P class="s570a4-10 iEJDri"&gt;It gives me this:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Variable_source&lt;/TD&gt;&lt;TD&gt;VARIABLE_variable_source&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01 03&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01 03 04&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01 05&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01 02&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="s570a4-10 iEJDri"&gt;But I want this:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Variable_source&lt;/TD&gt;&lt;TD&gt;VARIABLE_01&lt;/TD&gt;&lt;TD&gt;VARIABLE_02&lt;/TD&gt;&lt;TD&gt;VARIABLE_03&lt;/TD&gt;&lt;TD&gt;VARIABLE_04&lt;/TD&gt;&lt;TD&gt;VARIABLE_05&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01 03&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01 03 04&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01 05&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01 02&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Aug 2018 23:10:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Passing-a-Dynamic-Variable-from-Data-set-as-a-Macro-Parameter/m-p/485622#M71873</guid>
      <dc:creator>BadMojoNixon</dc:creator>
      <dc:date>2018-08-09T23:10:31Z</dc:date>
    </item>
    <item>
      <title>Re: Passing a Dynamic Variable from Data set as a Macro Parameter</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Passing-a-Dynamic-Variable-from-Data-set-as-a-Macro-Parameter/m-p/485710#M71884</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The reason your macro doesn't work is because the macro is compiled before the data step is processed, i.e. the countw(&amp;amp;temp) will translate into countw(Variable_source) = 1 (same with the %scan()-function).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is one way of doing it without using a macro:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Input;                                                                         
input Variable_source $15.;                          
datalines;                                                                          
01 03
01 03 04
01 05
01 02
;

/* Looping through each dynamic array (using scan() to loop through the "words" and outputing each source as a new row) */
/* Creating dummy variable transpose_var=1 for transpose below and temporary variable rownumber to avoid sorting issues when transposing */
data work.input_spliced(drop=i);
	set work.input;
	rownumber = _n_;
	transpose_var = 1;
	do i = 1 to countw(Variable_source);
		Var_source_spliced = scan(Variable_source,i);
		output;
	end;
run;

/* Transposing the spliced table with variable prefix VARIABLE_ */
proc transpose data=work.input_spliced out=work.output(drop=rownumber _name_) prefix=VARIABLE_;
	by rownumber Variable_source;
	id Var_source_spliced;
	var transpose_var;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kind regards&lt;/P&gt;&lt;P&gt;/Calle&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit: Realized there was some unnecessary code in there, so I trimmed the solution a bit. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Aug 2018 12:50:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Passing-a-Dynamic-Variable-from-Data-set-as-a-Macro-Parameter/m-p/485710#M71884</guid>
      <dc:creator>CalleJ</dc:creator>
      <dc:date>2018-08-10T12:50:41Z</dc:date>
    </item>
  </channel>
</rss>

