<?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: Extract values from macro variable into a variabe in data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extract-values-from-macro-variable-into-a-variabe-in-data/m-p/983816#M379584</link>
    <description>&lt;P&gt;Become your own macro processor to see what the problem is.&amp;nbsp; Replace the macro variable reference in your code with its value and check if the result is valid SAS code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data names_data;
    length Name $5; 
     do _n_ = 1 to countw("AAA", "BBB", "CCC", ',');
         Name = scan(""AAA", "BBB", "CCC"", _n_, ',');
         output; 
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So the COUNTW() function call has way too many arguments.&lt;/P&gt;
&lt;P&gt;Let's add spaces to the SCAN() function call&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;scan(""  AAA  ", "  BBB  ", "  CCC  "", _n_, ',')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to see how it will be parsed.&lt;/P&gt;
&lt;P&gt;The first argument consists of alternating string literals and variable names which is not a valid expression.&amp;nbsp; In addition the variables AAA, BBB, and CCC do not exist.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can simple use the macro variable to generate the DO statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data names_data;
    length Name $5; 
     do Name  = &amp;amp;myNames ;
         output; 
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That will generate this valid SAS code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data names_data;
    length Name $5; 
     do Name  = "AAA", "BBB", "CCC" ;
         output; 
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 20 Feb 2026 23:42:48 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2026-02-20T23:42:48Z</dc:date>
    <item>
      <title>Extract values from macro variable into a variabe in data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-values-from-macro-variable-into-a-variabe-in-data/m-p/983815#M379583</link>
      <description>&lt;P&gt;I have a set of character values separated by commas in a macro variable.&amp;nbsp; I would like to create a SAS dataset with each observation taking one value from the macro variable. I run the following code but it gives an error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let myNames = "AAA", "BBB", "CCC", "V123";

data names_data;
    length Name $5; 
     do _n_ = 1 to countw(&amp;amp;myNames, ',');
         Name = scan("&amp;amp;myNames", _n_, ',');
         output; 
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Feb 2026 23:41:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-values-from-macro-variable-into-a-variabe-in-data/m-p/983815#M379583</guid>
      <dc:creator>PamG</dc:creator>
      <dc:date>2026-02-20T23:41:23Z</dc:date>
    </item>
    <item>
      <title>Re: Extract values from macro variable into a variabe in data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-values-from-macro-variable-into-a-variabe-in-data/m-p/983816#M379584</link>
      <description>&lt;P&gt;Become your own macro processor to see what the problem is.&amp;nbsp; Replace the macro variable reference in your code with its value and check if the result is valid SAS code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data names_data;
    length Name $5; 
     do _n_ = 1 to countw("AAA", "BBB", "CCC", ',');
         Name = scan(""AAA", "BBB", "CCC"", _n_, ',');
         output; 
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So the COUNTW() function call has way too many arguments.&lt;/P&gt;
&lt;P&gt;Let's add spaces to the SCAN() function call&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;scan(""  AAA  ", "  BBB  ", "  CCC  "", _n_, ',')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to see how it will be parsed.&lt;/P&gt;
&lt;P&gt;The first argument consists of alternating string literals and variable names which is not a valid expression.&amp;nbsp; In addition the variables AAA, BBB, and CCC do not exist.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can simple use the macro variable to generate the DO statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data names_data;
    length Name $5; 
     do Name  = &amp;amp;myNames ;
         output; 
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That will generate this valid SAS code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data names_data;
    length Name $5; 
     do Name  = "AAA", "BBB", "CCC" ;
         output; 
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Feb 2026 23:42:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-values-from-macro-variable-into-a-variabe-in-data/m-p/983816#M379584</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-02-20T23:42:48Z</dc:date>
    </item>
    <item>
      <title>Re: Extract values from macro variable into a variabe in data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-values-from-macro-variable-into-a-variabe-in-data/m-p/983817#M379585</link>
      <description>&lt;P&gt;You will find that having commas in macro variables is often a problem because that is the separator between values in the macro processor and when such macro variables are used in data step processing you will get similar problems except for the functions that lists of values that are separated by commas.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Additionally having quote characters as part of the value often complicates things as most functions will expect one value within a set of quotes. You encounter an example with your use of Scan ("&amp;amp;mynames",_n_,',') where the data step will see Scan (""AAA","BBB","CCC","V123"",_n_,','). So the first pair of "" is the empty string to search with everything else problematic as AAA" will not be a valid second parameter because of no comma.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why was your Myname macro variable defined that way? If only to be parsed by this data step then other definitions will likely work better.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Feb 2026 23:55:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-values-from-macro-variable-into-a-variabe-in-data/m-p/983817#M379585</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2026-02-20T23:55:38Z</dc:date>
    </item>
    <item>
      <title>Re: Extract values from macro variable into a variabe in data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-values-from-macro-variable-into-a-variabe-in-data/m-p/983818#M379586</link>
      <description>Thanks much, Tom.</description>
      <pubDate>Fri, 20 Feb 2026 23:57:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-values-from-macro-variable-into-a-variabe-in-data/m-p/983818#M379586</guid>
      <dc:creator>PamG</dc:creator>
      <dc:date>2026-02-20T23:57:58Z</dc:date>
    </item>
  </channel>
</rss>

