<?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: Reference all variables whose names start with the same prefix that is numeric in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Reference-all-variables-whose-names-start-with-the-same-prefix/m-p/804762#M316959</link>
    <description>The variable names have to be in the form 20YYMM without the "_", not by my choice.&lt;BR /&gt;I appreciate your time, thank you for responding.</description>
    <pubDate>Tue, 29 Mar 2022 14:03:18 GMT</pubDate>
    <dc:creator>GeorgeBonanza</dc:creator>
    <dc:date>2022-03-29T14:03:18Z</dc:date>
    <item>
      <title>Reference all variables whose names start with the same prefix that is numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reference-all-variables-whose-names-start-with-the-same-prefix/m-p/804729#M316939</link>
      <description>&lt;P&gt;I have a dataset that has month values in 20YYMM format as variable names that I want to reference using an array.&amp;nbsp; I know that if the variable names started with a common character string such as "COL" then I can reference them using COL: but 20: doesn't seem to work.&amp;nbsp; The dates will change so the variable reference needs to be dynamic. I can't use the _numeric_ keyword since there are other numeric variables.&amp;nbsp; Below is a sample dataset and you can see that the last data step causes an error.&amp;nbsp; Any insight is appreciated.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;options validvarname=any;

data work.DATA;
	input PRODUCT MONTH $ AMT;
	datalines;
1 202201 100
1 202202 175
2 202202 200
;
run;

proc transpose
	data= work.DATA
	out=  work.HAVE (drop= _NAME_);
	id MONTH; by PRODUCT; var AMT;
run;

data work.WANT;
	set work.HAVE;
	array MONTHS 20:;

	do over MONTHS;
	/* some stuff */
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Mar 2022 12:22:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reference-all-variables-whose-names-start-with-the-same-prefix/m-p/804729#M316939</guid>
      <dc:creator>GeorgeBonanza</dc:creator>
      <dc:date>2022-03-29T12:22:25Z</dc:date>
    </item>
    <item>
      <title>Re: Reference all variables whose names start with the same prefix that is numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reference-all-variables-whose-names-start-with-the-same-prefix/m-p/804737#M316947</link>
      <description>&lt;P&gt;Is there a reason you're using `options validvarname = any`? If you don't use that, SAS will prepend it with an underscore. You can then use that pattern to do whatever you need to do in your DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options validvarname=v7;

data work.DATA;
	input PRODUCT MONTH $ AMT;
	datalines;
1 202201 100
1 202202 175
2 202202 200
;
run;

proc transpose
	data= work.DATA
	out=  work.HAVE (drop= _NAME_);
	id MONTH; by PRODUCT; var AMT;
run;

data work.WANT;
	set work.HAVE;
	array MONTHS [*] _:;

	do i = 1 to dim(MONTHS);
		x = MONTHS[i] + 1;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you need to impose the date values as columns, maybe do it after your DATA step? Then write it out? I know certain people prefer to have reports made a certain way, so that may be the case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options validvarname = any;

data want2;
set want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Mar 2022 12:44:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reference-all-variables-whose-names-start-with-the-same-prefix/m-p/804737#M316947</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2022-03-29T12:44:11Z</dc:date>
    </item>
    <item>
      <title>Re: Reference all variables whose names start with the same prefix that is numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reference-all-variables-whose-names-start-with-the-same-prefix/m-p/804741#M316950</link>
      <description>&lt;P&gt;Make a macro variable to contain it .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;options validvarname=any;

data work.DATA;
	input PRODUCT MONTH $ AMT;
	datalines;
1 202201 100
1 202202 175
2 202202 200
;
run;

proc transpose
	data= work.DATA
	out=  work.HAVE (drop= _NAME_);
	id MONTH; by PRODUCT; var AMT;
run;

proc sql noprint;
select distinct nliteral(month) into : months separated by ' '
 from data;
quit;

data work.WANT;
	set work.HAVE;
	array MONTHS{*} &amp;amp;months. ;
 put MONTHS{1}=;
 put MONTHS{2}=;

run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Mar 2022 13:03:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reference-all-variables-whose-names-start-with-the-same-prefix/m-p/804741#M316950</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-03-29T13:03:13Z</dc:date>
    </item>
    <item>
      <title>Re: Reference all variables whose names start with the same prefix that is numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reference-all-variables-whose-names-start-with-the-same-prefix/m-p/804754#M316952</link>
      <description>&lt;P&gt;To make a variable list that references all variables that start with some prefix use the : wildcard.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set sashelp.class(obs=3);
  put (a:) (=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the variable names (or at least the prefix part) are NOT valid SAS names then use a name literal for the prefix.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set sashelp.class(obs=3);
  put ('a'n:) (=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really only want to include NUMERIC variables and there are both numeric and character variables whose names start with the prefix then perhaps you can read the dataset in pieces.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have(keep=_numeric_);
   array months&amp;nbsp;'20'n:&amp;nbsp;;
&amp;nbsp;&amp;nbsp;&amp;nbsp;set&amp;nbsp;have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This works because the variable list is evaluated when it is first seen and will only find the variables that have already been defined.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS To avoid the need for name literals set the system option VALIDVARNAME to V7 instead of ANY. Then procedures like PROC IMPORT or PROC TRANSPOSE will not create invalid variable names to begin with.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2022 13:36:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reference-all-variables-whose-names-start-with-the-same-prefix/m-p/804754#M316952</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-03-29T13:36:28Z</dc:date>
    </item>
    <item>
      <title>Re: Reference all variables whose names start with the same prefix that is numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reference-all-variables-whose-names-start-with-the-same-prefix/m-p/804762#M316959</link>
      <description>The variable names have to be in the form 20YYMM without the "_", not by my choice.&lt;BR /&gt;I appreciate your time, thank you for responding.</description>
      <pubDate>Tue, 29 Mar 2022 14:03:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reference-all-variables-whose-names-start-with-the-same-prefix/m-p/804762#M316959</guid>
      <dc:creator>GeorgeBonanza</dc:creator>
      <dc:date>2022-03-29T14:03:18Z</dc:date>
    </item>
    <item>
      <title>Re: Reference all variables whose names start with the same prefix that is numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reference-all-variables-whose-names-start-with-the-same-prefix/m-p/804764#M316960</link>
      <description>I didn't know about the NLITERAL function. This worked and I will definitely use NLITERAL in future work. Thank you for responding, I appreciate your time.</description>
      <pubDate>Tue, 29 Mar 2022 14:05:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reference-all-variables-whose-names-start-with-the-same-prefix/m-p/804764#M316960</guid>
      <dc:creator>GeorgeBonanza</dc:creator>
      <dc:date>2022-03-29T14:05:56Z</dc:date>
    </item>
  </channel>
</rss>

