<?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: Can a SAS array be used to reference the middle few characters of a variable name? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Can-a-SAS-array-be-used-to-reference-the-middle-few-characters/m-p/962420#M375106</link>
    <description>&lt;P&gt;Not sure the XLSX file helps much.&amp;nbsp; It just seems to be a list of variable names.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1742583315692.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/105580iC3DE9676588BE494/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1742583315692.png" alt="Tom_0-1742583315692.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;That would have been better shared as plain text so it was more easily seen and copied.&lt;/P&gt;
&lt;P&gt;And it would help if the result was also provided.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So your macro recreating the same dataset over and over.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.example1;
  set work.example;
data work.example1;
  set work.example;
data work.example1;
  set work.example;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So at the end of the macro you will have just the LAST version of EXAMPLE1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is it that you are trying to do?&amp;nbsp; Perhaps you just need to move the %DO loop so it is generating a series of statement inside of ONE data step.&amp;nbsp; At least then you will have a chance of changing more than one variable's values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.example1;
  set work.example;
%do .....

%end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 21 Mar 2025 19:00:21 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2025-03-21T19:00:21Z</dc:date>
    <item>
      <title>Can a SAS array be used to reference the middle few characters of a variable name?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-a-SAS-array-be-used-to-reference-the-middle-few-characters/m-p/962022#M375011</link>
      <description>&lt;P&gt;I have searched around, but no luck finding an answer (perhaps I am using the wrong terms).&lt;/P&gt;&lt;P&gt;I have arrays set up to reference full lists of variables, but they are becoming quite bulky. Is it possible to reference only a few characters in the middle of a variable name?&lt;/P&gt;&lt;P&gt;In stata it would look like this:&lt;/P&gt;&lt;P&gt;for each x in&amp;nbsp; cath fall {&lt;/P&gt;&lt;P&gt;use `datafile', clear&lt;/P&gt;&lt;P&gt;collapse (mean) measure_`x'_value ///&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; pred_measure_`x'_value&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;[aweight = measure_`x'_count], by (state)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am not worried about the collapse or aweight portions,&amp;nbsp; but I am trying to find out if it is possible to replicate that reference to `x' and cycle through just the middle of the variable name. In stata, the `x' would be replaced to measure_cath_value and measure_fall_value etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for any help!&lt;/P&gt;</description>
      <pubDate>Mon, 17 Mar 2025 17:52:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-a-SAS-array-be-used-to-reference-the-middle-few-characters/m-p/962022#M375011</guid>
      <dc:creator>Zen_aly</dc:creator>
      <dc:date>2025-03-17T17:52:34Z</dc:date>
    </item>
    <item>
      <title>Re: Can a SAS array be used to reference the middle few characters of a variable name?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-a-SAS-array-be-used-to-reference-the-middle-few-characters/m-p/962024#M375012</link>
      <description>&lt;P&gt;Since you are having trouble explaining in word try providing an example.&amp;nbsp; Show a dataset with three or for variable names and explain what you want to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to select variable names you need to do that BEFORE the data step is compiled.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The simplest way is to put the list of variable names into a macro variable which you can then use to generate the list of names needed for the ARRAY statement.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example if you wanted the names of all character variables that have the letter X in their name you could do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=have noprint out=contents; run;
proc sql noprint;
select nliteral(name) into :namelist separated by ' '
from contents
where upcase(name) like '%X%'
  and type=2
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could then use that macro variable to help you write a data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array x &amp;amp;namelist ;
* some code that references the array X ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 Mar 2025 18:07:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-a-SAS-array-be-used-to-reference-the-middle-few-characters/m-p/962024#M375012</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-03-17T18:07:36Z</dc:date>
    </item>
    <item>
      <title>Re: Can a SAS array be used to reference the middle few characters of a variable name?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-a-SAS-array-be-used-to-reference-the-middle-few-characters/m-p/962038#M375015</link>
      <description>&lt;P&gt;For this, you'd probably just want use a macro in SAS (basically, an .ado file in Stata - &lt;EM&gt;not&lt;/EM&gt; what Stata calls a macro).&amp;nbsp; To do what you're after, try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro loop (varlist=);
  %let varlist=%cmpres(&amp;amp;varlist);
  %do i=1 %to %sysfunc(countW(&amp;amp;varlist, ' '));
    %let x=%scan(&amp;amp;varlist,&amp;amp;i,' ');
    ** your code here -- just reference x by preceding with ampersand ;
    ** for example... ;
    proc means data=some_dataset;
    var measure_&amp;amp;x._value;
    output out=avg mean=mean_&amp;amp;x;
    run;
 %end;
%mend;  *loop();

%let cath=var1 var2 var3 var4 var5;

%loop(varlist=&amp;amp;cath);
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 Mar 2025 01:35:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-a-SAS-array-be-used-to-reference-the-middle-few-characters/m-p/962038#M375015</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-03-18T01:35:20Z</dc:date>
    </item>
    <item>
      <title>Re: Can a SAS array be used to reference the middle few characters of a variable name?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-a-SAS-array-be-used-to-reference-the-middle-few-characters/m-p/962413#M375105</link>
      <description>&lt;P&gt;Thanks for the responses! I am adding an excel file and some code that I tried based on responses:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;%macro loop (varlist=);&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; %let varlist=%cmpres(&amp;amp;varlist);&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; %do i=1 %to %sysfunc(countW(&amp;amp;varlist, ' '));&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; %let x=%scan(&amp;amp;varlist,&amp;amp;i,' ');&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp;&lt;SPAN&gt;&amp;nbsp; data work.example1;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;set work.example;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if . &amp;lt; Measure_&amp;amp;x._value &amp;lt; 0 then Measure_&amp;amp;x._value=0; &lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;else if Measure_&amp;amp;x._value &amp;gt; 40 then Measure_&amp;amp;x._value=40;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if visit_&amp;amp;x._count &amp;lt; 40 then do;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;Measure_&amp;amp;x._value=.;&lt;/DIV&gt;&lt;DIV&gt;visit_&amp;amp;x._count=0;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; %end;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%mend;&amp;nbsp; *loop();&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;%let CY25=100 abc def 222 ghi jklmno 300 350 399 pqrs;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;%loop(varlist=&amp;amp;CY25);&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The above code runs with no error but the changes I asked for did not take place. I just don't seem to get some of the details that I need to successfully create a wide variety of macros.&lt;/DIV&gt;</description>
      <pubDate>Fri, 21 Mar 2025 18:00:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-a-SAS-array-be-used-to-reference-the-middle-few-characters/m-p/962413#M375105</guid>
      <dc:creator>Zen_aly</dc:creator>
      <dc:date>2025-03-21T18:00:25Z</dc:date>
    </item>
    <item>
      <title>Re: Can a SAS array be used to reference the middle few characters of a variable name?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-a-SAS-array-be-used-to-reference-the-middle-few-characters/m-p/962420#M375106</link>
      <description>&lt;P&gt;Not sure the XLSX file helps much.&amp;nbsp; It just seems to be a list of variable names.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1742583315692.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/105580iC3DE9676588BE494/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1742583315692.png" alt="Tom_0-1742583315692.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;That would have been better shared as plain text so it was more easily seen and copied.&lt;/P&gt;
&lt;P&gt;And it would help if the result was also provided.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So your macro recreating the same dataset over and over.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.example1;
  set work.example;
data work.example1;
  set work.example;
data work.example1;
  set work.example;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So at the end of the macro you will have just the LAST version of EXAMPLE1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is it that you are trying to do?&amp;nbsp; Perhaps you just need to move the %DO loop so it is generating a series of statement inside of ONE data step.&amp;nbsp; At least then you will have a chance of changing more than one variable's values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.example1;
  set work.example;
%do .....

%end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Mar 2025 19:00:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-a-SAS-array-be-used-to-reference-the-middle-few-characters/m-p/962420#M375106</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-03-21T19:00:21Z</dc:date>
    </item>
  </channel>
</rss>

