<?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: SAS Macrovariables inside a Data Step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macrovariables-inside-a-Data-Step/m-p/838794#M331656</link>
    <description>&lt;P&gt;Hello, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; I don't understand your answer, and when I run the code without the extra semi-colon that you would insert, I get a clean log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let var_list=height weight age;
options mprint;
%macro getArrayData(dat);
data &amp;amp;dat;
    %do i=1 %to 3;
       array %scan(&amp;amp;var_list,&amp;amp;i.)_var {4};
    %end;
    set sashelp.class;
run;
%mend getArrayData;

%getArrayData(want)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Log:&lt;/P&gt;
&lt;PRE&gt; 69         %let var_list=height weight age;
 70         options mprint;
 71         %macro getArrayData(dat);
 72         data &amp;amp;dat;
 73         %do i=1 %to 3;
 74                array %scan(&amp;amp;var_list,&amp;amp;i.)_var {4};
 75         %end;
 76         set sashelp.class;
 77         run;
 78         %mend getArrayData;
 79         
 80         %getArrayData(want)
 MPRINT(GETARRAYDATA):   data want;
 MPRINT(GETARRAYDATA):   array height_var {4};
 MPRINT(GETARRAYDATA):   array weight_var {4};
 MPRINT(GETARRAYDATA):   array age_var {4};
 MPRINT(GETARRAYDATA):   set sashelp.class;
 MPRINT(GETARRAYDATA):   run;

 NOTE: There were 19 observations read from the data set SASHELP.CLASS.
 NOTE: The data set WORK.WANT has 19 observations and 17 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       user cpu time       0.00 seconds
       system cpu time     0.00 seconds&lt;/PRE&gt;</description>
    <pubDate>Sat, 15 Oct 2022 10:14:15 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2022-10-15T10:14:15Z</dc:date>
    <item>
      <title>SAS Macrovariables inside a Data Step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macrovariables-inside-a-Data-Step/m-p/838762#M331638</link>
      <description>&lt;P&gt;I have a macrovariable&amp;nbsp; &lt;FONT size="2"&gt;&lt;STRONG&gt;&amp;amp;var_list&lt;/STRONG&gt;&lt;/FONT&gt; which stores a list of integer variable names from the dataset &lt;FONT size="2"&gt;&lt;STRONG&gt;have&lt;/STRONG&gt;&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;&lt;EM&gt;&amp;gt;&amp;gt;%put &amp;amp;var_list.;&lt;/EM&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;&lt;EM&gt;id trt grade&lt;/EM&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Now I want to create a new dataset &lt;STRONG&gt;&lt;FONT size="2"&gt;want&lt;/FONT&gt;&lt;/STRONG&gt; where each of the above variable names itself becomes an array. I want macro code that creates a new data set where all the existing variables become arrays of length 4. Like so:&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;&lt;EM&gt;data want;&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;&lt;EM&gt;array id_var{4};&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;&lt;EM&gt;array trt_var{4};&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;&lt;EM&gt;array grade_var{4};&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;&lt;EM&gt;set have;&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;&lt;EM&gt;run;&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;I have tried the following macro code but it doesn't run correctly. I am trying to iterate through &amp;amp;var_list, concatenate the string "var" to the variable name and also add a {4} at the end to declare each variable as an array. But SAS complains that the keyword "array" is itself an undeclared array variable. It doesn't realise that I am trying to use the keyword &lt;STRONG&gt;&lt;FONT size="2"&gt;array&lt;/FONT&gt;&lt;/STRONG&gt; inside the %do macro. Also, what do I do about the {4} in the declaration statement?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;&lt;EM&gt;&amp;gt;&amp;gt;%put &amp;amp;var_list.;&lt;/EM&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT size="2"&gt;&lt;EM&gt;id trt grade&lt;/EM&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;&lt;EM&gt;%macro getArrayData(dat);&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;&lt;EM&gt;data &amp;amp;dat;&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;&lt;EM&gt;%do i=1 %to 3;&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;&lt;EM&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;array %scan(&amp;amp;var_list,&amp;amp;i.)_var {4};&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;&lt;EM&gt;%end;&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;&lt;EM&gt;set have;&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;&lt;EM&gt;run;&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;&lt;EM&gt;%mend getArrayData;&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;%&lt;FONT size="2"&gt;&lt;STRONG&gt;&lt;EM&gt;getArrayData&lt;/EM&gt;(want);&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 15 Oct 2022 03:47:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macrovariables-inside-a-Data-Step/m-p/838762#M331638</guid>
      <dc:creator>nstdt</dc:creator>
      <dc:date>2022-10-15T03:47:14Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macrovariables inside a Data Step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macrovariables-inside-a-Data-Step/m-p/838786#M331654</link>
      <description>&lt;P&gt;Will it make you feel better if I tell you that you were one semicolon short of a solution? The semicolon immediately after the {4} is basically ending the statement in the macro using the %scan. You need one more to complete the Array statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro getArrayData(dat);

data &amp;amp;dat;
%do i=1 %to 3;
       array %scan(&amp;amp;var_list,&amp;amp;i.)_var {4};    &lt;FONT color="#FF00FF"&gt;&lt;STRONG&gt;&lt;FONT size="6"&gt; ;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;
%end;
set have;
run;
%mend getArrayData;
&lt;/PRE&gt;
&lt;P&gt;If you ran the original version after setting OPTIONS MPRINT you might notice the generated code was missing the semicolon for the Array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Suggestion: provide your VAR_LIST values as a parameter to the macro. Having a magic macro variable just appear in the code body as in the %scan function means that reusing the code later may fail because you don't remember when/where/how varlist was created.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also make this more dynamic using&lt;/P&gt;
&lt;PRE&gt;%do i=1 %to %sysfunc(countw(&amp;amp;var_list.));&lt;/PRE&gt;
&lt;P&gt;Sysfunc allows use of data step functions like Countw, which will count the number of space delimited "words", i.e. variables are in the list. So you don't have to figure out why array ABC_var is not created when you add it to Var_list. Or why you get an error when Var_list only has 2 variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 15 Oct 2022 06:01:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macrovariables-inside-a-Data-Step/m-p/838786#M331654</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-10-15T06:01:20Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macrovariables inside a Data Step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macrovariables-inside-a-Data-Step/m-p/838794#M331656</link>
      <description>&lt;P&gt;Hello, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; I don't understand your answer, and when I run the code without the extra semi-colon that you would insert, I get a clean log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let var_list=height weight age;
options mprint;
%macro getArrayData(dat);
data &amp;amp;dat;
    %do i=1 %to 3;
       array %scan(&amp;amp;var_list,&amp;amp;i.)_var {4};
    %end;
    set sashelp.class;
run;
%mend getArrayData;

%getArrayData(want)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Log:&lt;/P&gt;
&lt;PRE&gt; 69         %let var_list=height weight age;
 70         options mprint;
 71         %macro getArrayData(dat);
 72         data &amp;amp;dat;
 73         %do i=1 %to 3;
 74                array %scan(&amp;amp;var_list,&amp;amp;i.)_var {4};
 75         %end;
 76         set sashelp.class;
 77         run;
 78         %mend getArrayData;
 79         
 80         %getArrayData(want)
 MPRINT(GETARRAYDATA):   data want;
 MPRINT(GETARRAYDATA):   array height_var {4};
 MPRINT(GETARRAYDATA):   array weight_var {4};
 MPRINT(GETARRAYDATA):   array age_var {4};
 MPRINT(GETARRAYDATA):   set sashelp.class;
 MPRINT(GETARRAYDATA):   run;

 NOTE: There were 19 observations read from the data set SASHELP.CLASS.
 NOTE: The data set WORK.WANT has 19 observations and 17 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       user cpu time       0.00 seconds
       system cpu time     0.00 seconds&lt;/PRE&gt;</description>
      <pubDate>Sat, 15 Oct 2022 10:14:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macrovariables-inside-a-Data-Step/m-p/838794#M331656</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-10-15T10:14:15Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macrovariables inside a Data Step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macrovariables-inside-a-Data-Step/m-p/838808#M331660</link>
      <description>&lt;P&gt;I think you must be misreading your log, or have a problem somewhere else in your SAS session.&amp;nbsp; As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;showed, if you start a new SAS session and run your code, it works fine.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have still have the log with the error message, please post it.&amp;nbsp; You might be misunderstanding the message.&lt;/P&gt;</description>
      <pubDate>Sat, 15 Oct 2022 11:59:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macrovariables-inside-a-Data-Step/m-p/838808#M331660</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-10-15T11:59:49Z</dc:date>
    </item>
  </channel>
</rss>

