<?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: How to create a macro variable containing an array/list of values? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790147#M32461</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp; That's a helpful explanation, thank you.&amp;nbsp; I guess I'm too used to Python, R, Java, and the like, where the type of an object is more important.&lt;/P&gt;</description>
    <pubDate>Fri, 14 Jan 2022 14:54:16 GMT</pubDate>
    <dc:creator>osbornejo</dc:creator>
    <dc:date>2022-01-14T14:54:16Z</dc:date>
    <item>
      <title>How to create a macro variable containing an array/list of values?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790080#M32446</link>
      <description>&lt;P&gt;I want to create a macro variable that contains multiple values to check against.&amp;nbsp; Example pseudocode:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%LET myCodes = [Not sure how to do this part];&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; /*Ideally &amp;amp;myCodes would return a list/array of the codes I wanted.&amp;nbsp; i.e. Code1, Code2,...*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; create table myTable as&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; select * from DataTable&amp;nbsp; &amp;nbsp; /*Note:&amp;nbsp; Assume DataTable has a column named Code*/&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; where Code in &amp;amp;myCodes;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/pseudocode&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I am having trouble with is defining myCodes.&amp;nbsp; I had the thought of just defining it as&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; %LET myCodes = Code1 Code2 ...;&lt;/P&gt;&lt;P&gt;but that would just make myCode be equivalent to a single long string, correct?&amp;nbsp; I'd imagine that has the potential to cause problems.&amp;nbsp; Even if I separated them (e.g. 'Code1','Code2',...) my understanding is that I would just end up with a string that contains all the punctuation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Am I misunderstanding something or is there a different technique to do this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Edit: The Codes are alphanumeric (e.g. A1234). The values in myCodes will be entered by hand. I'm trying to filter a dataset for observations with alphanumeric CODE that matches one of the codes in myCodes.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jan 2022 14:49:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790080#M32446</guid>
      <dc:creator>osbornejo</dc:creator>
      <dc:date>2022-01-14T14:49:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a macro variable containing an array/list of values?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790081#M32447</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If CODE is a numeric variable then just use a space delimited list:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mycodes=1 3 5 ;
....
  where code in (&amp;amp;mycodes)
....&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If CODE is a character variable then include quotes around the values:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mycodes='A' "B" 'C' ;
....
  where code in (&amp;amp;mycodes)
....&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Are you planning to type the list of codes by hand? Or generate it from a dataset?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have the list in a dataset then use PROC SQL to generate the list into a macro variable.&lt;/P&gt;
&lt;P&gt;For character values use the QUOTE() function to add the quotes.&amp;nbsp; No need to include the trailing spaces that SAS stores in its fixed length character variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select code into :mycodes separated by ' ' from mylist;
select quote(trim(code)) into :mycodes separated by ' ' from mylist;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jan 2022 23:33:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790081#M32447</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-13T23:33:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a macro variable containing an array/list of values?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790086#M32451</link>
      <description>&lt;P&gt;What are you intending to use the list for? There could be better ways to solve your use case if you explain what it is.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jan 2022 00:07:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790086#M32451</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-01-14T00:07:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a macro variable containing an array/list of values?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790105#M32453</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are lots of ways to create a macro variable containing a list of values.&amp;nbsp; The easiest is to just type the list.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think your question is partly about how macro variables work.&amp;nbsp; The key thing to keep in mind is that macro variables are for text substitution.&amp;nbsp; You can assign a text value to a macro variable, and when the macro variable resolves it will substitute that text into the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a valid SQL step with no macro variables&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
  create table class as
  select * from sashelp.class
  where Name IN ("Mary" "John") 
;
quit ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to store the list of students' names in a macro variable, you could do it like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let myNames="Mary" "John" ;

proc sql ;
  create table class as
  select * from sashelp.class
  where Name IN (&amp;amp;myNames) 
;
quit ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;When the PROC SQL step is compiling (or I guess being interpreted), the macro reference &amp;amp;myNames will resolve to "Mary" "John".&amp;nbsp; So both PROC SQL steps are exactly the same.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you're starting to work with macro variables, the key is to remember that all they are doing is text substitution.&amp;nbsp; And when you start writing macros, the key is to remember that all they are doing is text generation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope that helps,&lt;/P&gt;
&lt;P&gt;--Q.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jan 2022 03:25:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790105#M32453</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-01-14T03:25:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a macro variable containing an array/list of values?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790114#M32454</link>
      <description>&lt;P&gt;Rule #1 of macro development: start with working non-macro SAS code.&lt;/P&gt;
&lt;P&gt;So you first need to write down the working SQL code for your condition. Once you have that, it will be obvious what has to go into the macro variable. Keep in mind that the macro preprocessor is just a text replacement tool.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jan 2022 08:18:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790114#M32454</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-01-14T08:18:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a macro variable containing an array/list of values?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790143#M32458</link>
      <description>&lt;P&gt;&lt;STRONG&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13976"&gt;@SASKiwi&lt;/a&gt;&amp;nbsp;&lt;/STRONG&gt;I'm trying to use it to filter a dataset for observations with alphanumeric CODE that matches one of the codes in myCodes.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jan 2022 14:50:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790143#M32458</guid>
      <dc:creator>osbornejo</dc:creator>
      <dc:date>2022-01-14T14:50:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a macro variable containing an array/list of values?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790145#M32460</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp; CODE is alphanumeric, and I'll be entering myCodes by hand.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jan 2022 14:50:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790145#M32460</guid>
      <dc:creator>osbornejo</dc:creator>
      <dc:date>2022-01-14T14:50:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a macro variable containing an array/list of values?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790147#M32461</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp; That's a helpful explanation, thank you.&amp;nbsp; I guess I'm too used to Python, R, Java, and the like, where the type of an object is more important.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jan 2022 14:54:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790147#M32461</guid>
      <dc:creator>osbornejo</dc:creator>
      <dc:date>2022-01-14T14:54:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a macro variable containing an array/list of values?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790173#M32468</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/406934"&gt;@osbornejo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp; That's a helpful explanation, thank you.&amp;nbsp; I guess I'm too used to Python, R, Java, and the like, where the type of an object is more important.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The type of a variable in SAS code does make a difference.&amp;nbsp; That is why you need to place quotes around the values so the SAS interpreter will see them as string literals instead of variable names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the macro processor is not a programming language.&amp;nbsp; It is just as its name suggests a processor for expanding the text before it is passed onto the real language to be compiled/interpreted.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jan 2022 16:20:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790173#M32468</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-14T16:20:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a macro variable containing an array/list of values?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790180#M32469</link>
      <description>Exactly, the macro language does not have objects.  And it does not know anything about SAS datasets or variables that exist in SAS datasets. &lt;BR /&gt;&lt;BR /&gt;While we talk about "macro variable lists" and even "macro arrays" the values are always just text strings, and sometimes those text strings are useful to think of as a list or array.  But that is all in the macro programmer's head. The macro language thinks everything is just text strings.</description>
      <pubDate>Fri, 14 Jan 2022 16:53:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-a-macro-variable-containing-an-array-list-of/m-p/790180#M32469</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-01-14T16:53:18Z</dc:date>
    </item>
  </channel>
</rss>

