<?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: Name New Variables Based on Existing in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Name-New-Variables-Based-on-Existing/m-p/825276#M325964</link>
    <description>&lt;P&gt;Do not add macro quoting to the name pulled from the list.&amp;nbsp; That will confuse the parser when into thinking there are two names instead of one.&amp;nbsp; Anything that can be a valid SAS variable name will not require macro quoting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you really want to make N separate datasets?&amp;nbsp; Or did you want to make N separate variables?&lt;/P&gt;
&lt;P&gt;If the latter then your %DO loop is in the wrong place.&lt;/P&gt;
&lt;P&gt;There is no need to deal with macro variable names with numeric suffixes for this problem.&amp;nbsp; Just re-use the same macro variable to hold the next name in the list.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro example(vars=,prefix=,suffix=);
%local i num_vars var;
%let num_vars = %sysfunc(countw(%superq(vars),%str( )));

data want;
  set have;

%do i = 1 %to &amp;amp;num_vars.;
  %let var = %scan(%superq(vars),&amp;amp;i,%str( ));

if &amp;amp;var &amp;gt; 1 then &amp;amp;prefix.&amp;amp;var.&amp;amp;suffix ="ok";

%end;
run;
%mend;

data have;
  set sashelp.class;
run;

options mprint;
%example(vars=age height weight,suffix=_cl);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;378  options mprint;
379  %example(vars=age height weight,suffix=_cl);
MPRINT(EXAMPLE):   data want;
MPRINT(EXAMPLE):   set have;
MPRINT(EXAMPLE):   if age &amp;gt; 1 then age_cl ="ok";
MPRINT(EXAMPLE):   if height &amp;gt; 1 then height_cl ="ok";
MPRINT(EXAMPLE):   if weight &amp;gt; 1 then weight_cl ="ok";
MPRINT(EXAMPLE):   run;

NOTE: There were 19 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 19 observations and 8 variables.
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 25 Jul 2022 15:46:44 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-07-25T15:46:44Z</dc:date>
    <item>
      <title>Name New Variables Based on Existing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Name-New-Variables-Based-on-Existing/m-p/825267#M325962</link>
      <description>&lt;P&gt;hi, i am struggle to give a name to a variable which will include the name of my initial variables&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My initial variables are var1 and var2 i want the below code to create a new using the existing names of var1/var2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, i want a variable which will be named as var1_cl&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't understand while the&amp;nbsp;&amp;nbsp;&amp;amp;&amp;amp;var&amp;amp;i.. resolved in my if statement, it is not resolved when it comes to names&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the issue is in the&amp;nbsp; &amp;amp;&amp;amp;var&amp;amp;i.._cl ="ok" statement where i receive the below error&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input var1 var2;
datalines;
3 5
run;

%macro example(vars=);

	%let num_vars = %sysfunc(countw(%superq(vars)));

	%do i = 1 %to &amp;amp;num_vars.;

		%let var&amp;amp;i. = %qscan(%superq(vars), &amp;amp;i., %str( ));
		%put var&amp;amp;i. = &amp;amp;&amp;amp;var&amp;amp;i..;

	%end;

%do i = 1 %to &amp;amp;num_vars.;


data test_&amp;amp;i;
set test;
if &amp;amp;&amp;amp;var&amp;amp;i.. &amp;gt; 1 then &amp;amp;&amp;amp;var&amp;amp;i.._cl ="ok";
run;

%end;
%mend;


%example(vars=var1 var2);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Jul 2022 15:23:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Name-New-Variables-Based-on-Existing/m-p/825267#M325962</guid>
      <dc:creator>Toni2</dc:creator>
      <dc:date>2022-07-25T15:23:47Z</dc:date>
    </item>
    <item>
      <title>Re: Name New Variables Based on Existing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Name-New-Variables-Based-on-Existing/m-p/825276#M325964</link>
      <description>&lt;P&gt;Do not add macro quoting to the name pulled from the list.&amp;nbsp; That will confuse the parser when into thinking there are two names instead of one.&amp;nbsp; Anything that can be a valid SAS variable name will not require macro quoting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you really want to make N separate datasets?&amp;nbsp; Or did you want to make N separate variables?&lt;/P&gt;
&lt;P&gt;If the latter then your %DO loop is in the wrong place.&lt;/P&gt;
&lt;P&gt;There is no need to deal with macro variable names with numeric suffixes for this problem.&amp;nbsp; Just re-use the same macro variable to hold the next name in the list.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro example(vars=,prefix=,suffix=);
%local i num_vars var;
%let num_vars = %sysfunc(countw(%superq(vars),%str( )));

data want;
  set have;

%do i = 1 %to &amp;amp;num_vars.;
  %let var = %scan(%superq(vars),&amp;amp;i,%str( ));

if &amp;amp;var &amp;gt; 1 then &amp;amp;prefix.&amp;amp;var.&amp;amp;suffix ="ok";

%end;
run;
%mend;

data have;
  set sashelp.class;
run;

options mprint;
%example(vars=age height weight,suffix=_cl);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;378  options mprint;
379  %example(vars=age height weight,suffix=_cl);
MPRINT(EXAMPLE):   data want;
MPRINT(EXAMPLE):   set have;
MPRINT(EXAMPLE):   if age &amp;gt; 1 then age_cl ="ok";
MPRINT(EXAMPLE):   if height &amp;gt; 1 then height_cl ="ok";
MPRINT(EXAMPLE):   if weight &amp;gt; 1 then weight_cl ="ok";
MPRINT(EXAMPLE):   run;

NOTE: There were 19 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 19 observations and 8 variables.
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2022 15:46:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Name-New-Variables-Based-on-Existing/m-p/825276#M325964</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-07-25T15:46:44Z</dc:date>
    </item>
    <item>
      <title>Re: Name New Variables Based on Existing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Name-New-Variables-Based-on-Existing/m-p/825286#M325970</link>
      <description>&lt;P&gt;Note sure why you are using %qscan (much less %superq)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This doesn't throw the errors:&lt;/P&gt;
&lt;PRE&gt;%macro example(vars=);

	%let num_vars = %sysfunc(countw(%superq(vars)));

	%do i = 1 %to &amp;amp;num_vars.;

		%let var&amp;amp;i. = %scan(%superq(vars), &amp;amp;i.);
		%put var&amp;amp;i. = &amp;amp;&amp;amp;var&amp;amp;i.;

	%end;

%do i = 1 %to &amp;amp;num_vars.;


data test_&amp;amp;i;
set test;
if &amp;amp;&amp;amp;var&amp;amp;i.. &amp;gt; 1 then &amp;amp;&amp;amp;var&amp;amp;i.._cl ="ok";
run;

%end;
%mend;


%example(vars=var1 var2);&lt;/PRE&gt;
&lt;P&gt;So I suspect that the Qscan is partially the issue in how it protects the expected "special or mnemonic characters", i.e. the &amp;amp; or %&lt;/P&gt;
&lt;P&gt;If you are expecting to use variable names with the special characters, or indeed anything other that character, digit and underscore you are referencing the variables incorrectly everywhere as they would have to be in the name literal form of "some%name*char"n , quotes plus the n.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2022 16:05:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Name-New-Variables-Based-on-Existing/m-p/825286#M325970</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-07-25T16:05:58Z</dc:date>
    </item>
    <item>
      <title>Re: Name New Variables Based on Existing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Name-New-Variables-Based-on-Existing/m-p/825290#M325974</link>
      <description>great, thanks</description>
      <pubDate>Mon, 25 Jul 2022 16:19:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Name-New-Variables-Based-on-Existing/m-p/825290#M325974</guid>
      <dc:creator>Toni2</dc:creator>
      <dc:date>2022-07-25T16:19:40Z</dc:date>
    </item>
  </channel>
</rss>

