<?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: Loop over variables with name ending character. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loop-over-variables-with-name-ending-character/m-p/465367#M118697</link>
    <description>&lt;P&gt;While I would use &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;'s solution, leveraging the information in the sql-only dictionary table, it might be informative to consider a preliminary data step to do the same.&amp;nbsp;&amp;nbsp;Use that data step to write an array declaration to a temporary file, which is subsequently &lt;EM&gt;&lt;STRONG&gt;%INCLUDEd&lt;/STRONG&gt;&lt;/EM&gt; in your final data step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename t temp;
data _null_;
  set have (obs=1);
  array candidates{*} v1a--x3;
  file t;

  put 'array myvars {*} ';
  do i=1 to dim(candidates);
    _name=vname(candidates{i});
    if not(anydigit(char(_name,length(_name)))) then put +1 _name @;
  end;
  put  ';';
run;


options source2;
data want;
  set have;
  %include tmp;

  do i=1 to dim(myvars);
    ** do your array element processing here **;
  end;
  
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "options source2" tells SAS to print to the log all sas statements in the %included file.&lt;/P&gt;</description>
    <pubDate>Sun, 27 May 2018 22:16:45 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2018-05-27T22:16:45Z</dc:date>
    <item>
      <title>Loop over variables with name ending character.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-over-variables-with-name-ending-character/m-p/465300#M118677</link>
      <description>&lt;P&gt;Hi, I'm trying to work out if it's possible to use arrays and loops to parse over or reference variables that don't end in a number?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My variable names would look something like&lt;/P&gt;&lt;PRE&gt;v1a v1b v1c x1 v2a v2b v2c x2 v3a v3b v3c x3&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm interested in parsing over those variables starting with v with the use of arrays and loops but i'm having a bit of difficulty. This is completely incorrect as far as Sas is concerned. I can't even reference v[i]b.&lt;/P&gt;&lt;PRE&gt;array v[3] = v1a--v1c;
do i = 1 to 3;
    if v[i]b=1 or v[i]c=1 then output_variable=1;

end;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The other thought that came to me was to put the entire set of variables in an array, iterate over them and use vname() but I can't seem to get the proper understanding for how to make that work either.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Should I suck it up and manually code the variable names, or is it&amp;nbsp; possible to parse over variables with this naming structure using arrays?&lt;/P&gt;&lt;P&gt;I'd really like to tackle this as dynamically as possible.&lt;/P&gt;</description>
      <pubDate>Sun, 27 May 2018 02:23:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-over-variables-with-name-ending-character/m-p/465300#M118677</guid>
      <dc:creator>Scottmeup</dc:creator>
      <dc:date>2018-05-27T02:23:52Z</dc:date>
    </item>
    <item>
      <title>Re: Loop over variables with name ending character.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-over-variables-with-name-ending-character/m-p/465313#M118681</link>
      <description>&lt;P&gt;I'm not sure what you want to do with the variables, but you can get a list of variables not ending with a number from a data set like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data MyData;
input Var OtherVar Var1 Var2;
datalines;
1 2 3 4
run;

proc sql noprint;
select name into :vars separated by " " from dictionary.columns 
where memtype="DATA" and memname="MYDATA" and anydigit(strip(reverse(name))) ne 1;
quit;

%put &amp;amp;vars.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can then use &amp;amp;vars. to put the relevant variables in an array and loop over it like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data testing(drop=i);
   set MyData;
   array SomeArray {*} &amp;amp;vars.;
   do i=1 to dim(SomeArray);
      SomeArray[i]=100;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 May 2018 06:08:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-over-variables-with-name-ending-character/m-p/465313#M118681</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-05-27T06:08:24Z</dc:date>
    </item>
    <item>
      <title>Re: Loop over variables with name ending character.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-over-variables-with-name-ending-character/m-p/465329#M118685</link>
      <description>&lt;P&gt;Can you edit your question to show a few records of example data you HAVE, and also show the data you WANT?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's possible you could do what you want with a two-dimensional array.&amp;nbsp; Often if I am tempted to use a two-dimensional array, I take that as a sign that my data are poorly structured.&amp;nbsp; And I end up transposing the data into a format that will make it easier to work with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That said, a two-dimensional array approach might look something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input id v1a v1b v1c x1 v2a v2b v2c x2 v3a v3b v3c x3 ;
  cards ;
1  1  2  3  4  5  6  7  8  9  10  11  12
2 10 20 30 40 50 60 70 80 90 100 110 120
3  0  1  0  0  0  0  0  0  0   0   0   0
4  0  0  0  0  0  0  1  0  0   0   0   0
;

data want ;
  set have ;

  array v{3,3} v: ; *a little risky, assumes v* variables are in right order ; 

  do i=1 to dim1(v) ;

    *debugging, just to show how two-dimensional array works ;
    do j=1 to dim2(v) ;
      put id= v{i,j}= ;
    end ; 

    *v{i,2} will be v1b when i=1, v2b when i=2 etc ;
    if v{i,2}=1 or v{i,3}=1 then output_variable=1 ;
  end ;

run ;&lt;/CODE&gt;&lt;/PRE&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>Sun, 27 May 2018 12:42:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-over-variables-with-name-ending-character/m-p/465329#M118685</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2018-05-27T12:42:23Z</dc:date>
    </item>
    <item>
      <title>Re: Loop over variables with name ending character.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-over-variables-with-name-ending-character/m-p/465331#M118687</link>
      <description>&lt;P&gt;Here is one way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input id v1a v1b v1c x1 v2a v2b v2c x2 v3a v3b v3c x3 ;
  cards ;
1  1  2  3  4  5  6  7  8  9  10  11  12
2 0 1 1 40 0 1 0 80 1 1 1 120
3  0  1  0  0  0  0  0  0  0   0   0   0
4  0  0  0  0  0  0  1  0  0   0   0   0
;

data want ;
  set have ;

  array a_v{*} v1a--x3 ;
  array output_variable(3);
  
  do i=1 to 3;
    output_variable(i)=0;
  end;
  
  do i=1 to dim(a_v) ;
    if anydigit(strip(reverse(vname(a_v(i))))) ne 1 then do;
      if a_v(i) eq 1 then output_variable(input(compress(vname(a_v(i)),,'kd'),8.))=1;
    end;
  end;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 May 2018 13:38:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-over-variables-with-name-ending-character/m-p/465331#M118687</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2018-05-27T13:38:12Z</dc:date>
    </item>
    <item>
      <title>Re: Loop over variables with name ending character.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-over-variables-with-name-ending-character/m-p/465367#M118697</link>
      <description>&lt;P&gt;While I would use &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;'s solution, leveraging the information in the sql-only dictionary table, it might be informative to consider a preliminary data step to do the same.&amp;nbsp;&amp;nbsp;Use that data step to write an array declaration to a temporary file, which is subsequently &lt;EM&gt;&lt;STRONG&gt;%INCLUDEd&lt;/STRONG&gt;&lt;/EM&gt; in your final data step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename t temp;
data _null_;
  set have (obs=1);
  array candidates{*} v1a--x3;
  file t;

  put 'array myvars {*} ';
  do i=1 to dim(candidates);
    _name=vname(candidates{i});
    if not(anydigit(char(_name,length(_name)))) then put +1 _name @;
  end;
  put  ';';
run;


options source2;
data want;
  set have;
  %include tmp;

  do i=1 to dim(myvars);
    ** do your array element processing here **;
  end;
  
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "options source2" tells SAS to print to the log all sas statements in the %included file.&lt;/P&gt;</description>
      <pubDate>Sun, 27 May 2018 22:16:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-over-variables-with-name-ending-character/m-p/465367#M118697</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-05-27T22:16:45Z</dc:date>
    </item>
    <item>
      <title>Re: Loop over variables with name ending character.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-over-variables-with-name-ending-character/m-p/465423#M118713</link>
      <description>&lt;P&gt;One possibility is to use a two-dimensional array, and use a macro to generate the variable names in the correct order, e.g.:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;%macro array_names(prefix,suffix1,suffix2);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; %local i j;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; %do i=1 %to %sysfunc(countw(&amp;amp;suffix1));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; %do j=1 %to %sysfunc(countw(&amp;amp;suffix2)); &amp;amp;prefix.%scan(&amp;amp;suffix1,&amp;amp;i)%scan(&amp;amp;suffix2,&amp;amp;j)%end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; %end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;%mend;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;　&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data _null_;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; retain v1a 1 v1b 2 v1c 3 x1 99 v2a 4 v2b 5 v2c 6 x2 999 v3a 7 v3b 8 v3c 9 x3 0;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; array v(3,3) %array_names(v,1 2 3,a b c);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; do i=1 to 3;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; do j=1 to 3;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; put v(i,j);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;I suggest using a macro because that is a good way of making sure you get the right variable names in the right order. So, instead of v[1]b you use v[1,2] etc.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 28 May 2018 08:29:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-over-variables-with-name-ending-character/m-p/465423#M118713</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-05-28T08:29:55Z</dc:date>
    </item>
  </channel>
</rss>

