<?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: passing variable to array in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/passing-variable-to-array/m-p/26356#M3871</link>
    <description>Fantastic, thanks Cynthia&lt;BR /&gt;
Sorry, I should have specified that this was in a data step, but you hit the nail on the head.  Is there a way of determining the size of var_1-var_5/6/7 outside the data step?</description>
    <pubDate>Thu, 19 Jun 2008 07:47:00 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2008-06-19T07:47:00Z</dc:date>
    <item>
      <title>passing variable to array</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/passing-variable-to-array/m-p/26354#M3869</link>
      <description>Hi. I have an array that varies in size which depends on another variable ("Var").  I am trying to define another array that is based on the size of this array by using the dim statement.  But SAS is not happy with what I have done:&lt;BR /&gt;
&lt;BR /&gt;
Array ArrayVar1{*} Var: ;&lt;BR /&gt;
%Let j = dim (ArrayVar1) ;&lt;BR /&gt;
Array ArrayVar2{&amp;amp;j} ;&lt;BR /&gt;
&lt;BR /&gt;
Any ideas on how to make this work?</description>
      <pubDate>Wed, 18 Jun 2008 14:43:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/passing-variable-to-array/m-p/26354#M3869</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-06-18T14:43:08Z</dc:date>
    </item>
    <item>
      <title>Re: passing variable to array</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/passing-variable-to-array/m-p/26355#M3870</link>
      <description>Hi:&lt;BR /&gt;
  I'm not exactly sure what you're trying to do. Is this a code snippet from a SAS Macro program or a code snippet from a DATA step program? At any rate, the %LET statement is a COMPILE time statement. No data is read at compile time, so there is no possibility that you will have a value for &amp;amp;J at compile time. However, there is a way to figure out what the dimension of the new array should be. It just takes making your macro variable --outside-- of the data step where you need to use the macro variable. This is because if you create a macro variable inside a data step (with a CALL SYMPUT), you cannot USE it until after a step boundary has been encountered.&lt;BR /&gt;
 &lt;BR /&gt;
Here's an example. I know that there are 3 numeric variables in SASHELP.CLASS. I can do this with %LET:&lt;BR /&gt;
[pre]&lt;BR /&gt;
    &lt;BR /&gt;
%let arrdim = 3;    /* set at compile time */&lt;BR /&gt;
               &lt;BR /&gt;
data testarr; &lt;BR /&gt;
  set sashelp.class;&lt;BR /&gt;
  array c_num (*) _numeric_;&lt;BR /&gt;
  array wombat (&amp;amp;arrdim) ;  &lt;BR /&gt;
  do i = 1 to dim(c_num);&lt;BR /&gt;
    wombat(i) = c_num(i);&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
              &lt;BR /&gt;
proc print data=testarr;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
But note that the %LET statement is OUTSIDE of the DATA step with the reference to &amp;amp;ARRDIM. What you probably want to do, I -think- is have SAS determine what number to use for the array dim without hard-coding the value.&lt;BR /&gt;
 &lt;BR /&gt;
  So, consider this data in WORK.MYCHARS:&lt;BR /&gt;
[pre]&lt;BR /&gt;
Obs         show         howmany    char1       char2    char3           char4         char5    char6&lt;BR /&gt;
                             &lt;BR /&gt;
 1     I Love Lucy          4       Lucy        Ricky    Ethel           Fred&lt;BR /&gt;
 2     Muppet Show          5       Kermit      Gonzo    Swedish Chef    Miss Piggy    Rowlf&lt;BR /&gt;
 3     Sesame Street        4       Big Bird    Elmo     Kermit          Oscar&lt;BR /&gt;
 4     The Odd Couple       2       Oscar       Felix&lt;BR /&gt;
    &lt;BR /&gt;
[/pre]&lt;BR /&gt;
  &lt;BR /&gt;
I have a SHOW variable and a count variable (HOWMANY) that tells me how many characters there are for each show. I also have CHAR1-CHAR6 in the data, but the show with the most characters only has 5 characters. &lt;BR /&gt;
 &lt;BR /&gt;
So, with this PROC SQL, I will find out the MAX number of characters and I will put the max number of characters into a macro variable called &amp;amp;CHARDIM. (I also could have calculated the HOWMANY variable when I read in the data and created the macro variable there, but you said you had a variable with the number.)&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
  select max(howmany) into :chardim&lt;BR /&gt;
  from work.mychars;&lt;BR /&gt;
quit;&lt;BR /&gt;
           &lt;BR /&gt;
%let chardim = &amp;amp;chardim;&lt;BR /&gt;
%put ************************;&lt;BR /&gt;
%put largest number of characters is &amp;amp;chardim;&lt;BR /&gt;
%put use this value in next data step;&lt;BR /&gt;
%put ************************;&lt;BR /&gt;
              &lt;BR /&gt;
[/pre]&lt;BR /&gt;
  Now, I have a value for &amp;amp;CHARDIM, and I can see it in the SAS Log after the SQL step runs:&lt;BR /&gt;
[pre]&lt;BR /&gt;
2145  %let chardim = &amp;amp;chardim;&lt;BR /&gt;
2146  %put ************************;&lt;BR /&gt;
************************&lt;BR /&gt;
2147  %put largest number of characters is &amp;amp;chardim;&lt;BR /&gt;
largest number of characters is 5&lt;BR /&gt;
2148  %put use this value in next data step;&lt;BR /&gt;
use this value in next data step&lt;BR /&gt;
2149  %put ************************;&lt;BR /&gt;
************************&lt;BR /&gt;
          &lt;BR /&gt;
[/pre]&lt;BR /&gt;
  &lt;BR /&gt;
Now, I want to run a program, where I use &amp;amp;CHARDIM to KEEP only CHAR1-CHAR5 and to make new variables NA1-NA5. Since &amp;amp;CHARDIM is now in the Global Symbol Table, I can use in in my program and it will be available when the program compiles (which I could prove by turning on the MPRINT and SYMBOLGEN options):&lt;BR /&gt;
[pre]&lt;BR /&gt;
options mprint symbolgen;&lt;BR /&gt;
data usedim;&lt;BR /&gt;
  keep show howmany char1-char&amp;amp;chardim na1-na&amp;amp;chardim;&lt;BR /&gt;
  set mychars;&lt;BR /&gt;
  array c_arr $ char1-char&amp;amp;chardim;&lt;BR /&gt;
  array newarr {&amp;amp;chardim} $12 na1-na&amp;amp;chardim;&lt;BR /&gt;
  do i = 1 to dim(c_arr);&lt;BR /&gt;
    if c_arr(i) = 'Kermit' then&lt;BR /&gt;
       newarr(i) = 'Best';&lt;BR /&gt;
    else if c_arr(i) = 'Oscar' then&lt;BR /&gt;
       newarr(i) = 'Messiest';&lt;BR /&gt;
    else if c_arr(i) = "Lucy" then&lt;BR /&gt;
       newarr(i) = 'Funny';&lt;BR /&gt;
    else if c_arr(i) = "Miss Piggy" then&lt;BR /&gt;
       newarr(i) = "Loves Kermit";&lt;BR /&gt;
    else if i le howmany then newarr(i) = 'OK';&lt;BR /&gt;
    else newarr(i) = ' ';&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
        &lt;BR /&gt;
options linesize=120;&lt;BR /&gt;
proc print data=usedim;&lt;BR /&gt;
title "Use chardim of &amp;amp;chardim to define the array and variables";&lt;BR /&gt;
title2 "And only keep vars: show, howmany, char1-char&amp;amp;chardim and na1-na&amp;amp;chardim";&lt;BR /&gt;
run;&lt;BR /&gt;
          &lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
But let's just take these 3 statements...I have this in the program:&lt;BR /&gt;
[pre]&lt;BR /&gt;
  keep show howmany char1-char&amp;amp;chardim na1-na&amp;amp;chardim;&lt;BR /&gt;
  ....&lt;BR /&gt;
  array c_arr $ char1-char&amp;amp;chardim;&lt;BR /&gt;
  array newarr {&amp;amp;chardim} $12 na1-na&amp;amp;chardim;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
The above statements get sent to the Macro Processor for resolution BEFORE compile time...so when the macro processor is done with those 3 statements, THIS is what the compiler sees and compiles and are the statements that finally execute:&lt;BR /&gt;
[pre]&lt;BR /&gt;
  keep show howmany char1-char5 na1-na5;&lt;BR /&gt;
  ....&lt;BR /&gt;
  array c_arr $ char1-char5;&lt;BR /&gt;
  array newarr {5} $12 na1-na5;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
Every place that the macro processor saw &amp;amp;CHARDIM, it "typed in" or substituted a "5" in the string that it was building for the compiler. Essentially, the macro processor used the value of &amp;amp;CHARDIM -- and the compiler only saw the resolved statements. So, the output from the proc print is:&lt;BR /&gt;
[pre]&lt;BR /&gt;
Use chardim of 5 to define the array and variables&lt;BR /&gt;
And only keep vars: show, howmany, char1-char5 and na1-na5&lt;BR /&gt;
     &lt;BR /&gt;
Obs       show       howmany  char1     char2  char3         char4       char5  na1       na2  na3   na4           na5&lt;BR /&gt;
   &lt;BR /&gt;
 1   I Love Lucy        4     Lucy      Ricky  Ethel         Fred               Funny     OK   OK    OK&lt;BR /&gt;
 2   Muppet Show        5     Kermit    Gonzo  Swedish Chef  Miss Piggy  Rowlf  Best      OK   OK    Loves Kermit  OK&lt;BR /&gt;
 3   Sesame Street      4     Big Bird  Elmo   Kermit        Oscar              OK        OK   Best  Messiest&lt;BR /&gt;
 4   The Odd Couple     2     Oscar     Felix                                   Messiest  OK&lt;BR /&gt;
   &lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
There are a couple of different ways I could have coded the DO loop -- I could have coded DO i = 1 to howmany; -- which probably would have simplified my IF statements...but this is a place to start with what I -think- you're trying to do.  Also remember that the ARRAY statement is only setting up a convenient reference for a group of related variables. The array "names" C_ARR or NEWARR are never saved in the SAS data set. In one program I could have the ARRAY named C_ARR, but in a different program, I could name the array KOALA and refer to the same variables that had been referenced as C_ARR(i) with KOALA(i):&lt;BR /&gt;
[pre]&lt;BR /&gt;
Program 1 has: &lt;BR /&gt;
   set work.mychars;&lt;BR /&gt;
   array KOALA $ char1-char6;&lt;BR /&gt;
  &lt;BR /&gt;
Program 2 has: &lt;BR /&gt;
    set work.mychars;&lt;BR /&gt;
    array C_ARR $ char1-char6;&lt;BR /&gt;
    &lt;BR /&gt;
[/pre]&lt;BR /&gt;
   &lt;BR /&gt;
In other words, SAS never keeps the ARRAY name. It only keeps the variables that were treated as an array for the duration of the program. For the above 2 programs, the arrays were both referring to the same variables. In some data base systems, ARRAYS are a physical data construct and information about the array is stored in the data base, but in SAS, the ARRAY information does not persist once the program is over....only the individual variable names persist.&lt;BR /&gt;
   &lt;BR /&gt;
cynthia</description>
      <pubDate>Thu, 19 Jun 2008 00:08:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/passing-variable-to-array/m-p/26355#M3870</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2008-06-19T00:08:20Z</dc:date>
    </item>
    <item>
      <title>Re: passing variable to array</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/passing-variable-to-array/m-p/26356#M3871</link>
      <description>Fantastic, thanks Cynthia&lt;BR /&gt;
Sorry, I should have specified that this was in a data step, but you hit the nail on the head.  Is there a way of determining the size of var_1-var_5/6/7 outside the data step?</description>
      <pubDate>Thu, 19 Jun 2008 07:47:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/passing-variable-to-array/m-p/26356#M3871</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-06-19T07:47:00Z</dc:date>
    </item>
    <item>
      <title>Re: passing variable to array</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/passing-variable-to-array/m-p/26357#M3872</link>
      <description>Hi:&lt;BR /&gt;
  SAS has an extensive group of "dictionary" tables or "sashelp" tables that contain information about the datasets. You can reference these files without opening the relevant dataset.&lt;BR /&gt;
 &lt;BR /&gt;
[pre]  &lt;BR /&gt;
title 'Dictionary.Columns info';&lt;BR /&gt;
proc sql;&lt;BR /&gt;
  select name, length, type, format&lt;BR /&gt;
    from dictionary.columns&lt;BR /&gt;
    where libname="WORK" and&lt;BR /&gt;
          memname="MYCHARS";&lt;BR /&gt;
run;&lt;BR /&gt;
                    &lt;BR /&gt;
proc print data=sashelp.vcolumn noobs;&lt;BR /&gt;
  title 'SASHELP.VCOLUMN info';&lt;BR /&gt;
  where libname="WORK" and memname="MYCHARS";&lt;BR /&gt;
  var name length type format;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
  &lt;BR /&gt;
There have been lots of user-group papers written on the topic of using SAS Dictionary tables and/or the SASHELP equivalent of the tables. A Google search should help you find some more examples of usage. &lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Thu, 19 Jun 2008 14:31:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/passing-variable-to-array/m-p/26357#M3872</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2008-06-19T14:31:15Z</dc:date>
    </item>
  </channel>
</rss>

