<?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 access information stored in macro variable to create new variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/access-information-stored-in-macro-variable-to-create-new/m-p/489512#M127843</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a set of about 300 different macro variables each storing a list of product codes and I also have a dataset, listing all used product codes and their counts etc.. The codes in the 300 macro variables are only a part of all possible product codes and group the product codes into bigger categories.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to access the product code information stored in the macro variables to create a new variable in my dataset, which takes certain values depending on the specific codes stored in the different macro variables? I tried the following code but this does not work. Does anyone have an idea how I can do it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data b; set a ;&lt;BR /&gt;druggroup='M';&lt;BR /&gt;%macro groupcodes;&lt;BR /&gt;%do j=1 %to 300; %let e=%scan(&amp;amp;varlist3,&amp;amp;j); %if Bcode in (&amp;amp;&amp;amp;x&amp;amp;e) %then druggroup="&amp;amp;e";&lt;BR /&gt;run;&lt;BR /&gt;%end; %mend; %groupcodes&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I&amp;nbsp; would like to have in the end is a dataset&amp;nbsp; which looks like the exmple below:&lt;/P&gt;&lt;P&gt;bcode druggroup&lt;/P&gt;&lt;P&gt;b123&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a&lt;/P&gt;&lt;P&gt;b234&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; b&lt;/P&gt;&lt;P&gt;b122&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a&lt;/P&gt;&lt;P&gt;b129&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a&lt;/P&gt;&lt;P&gt;b255&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c&lt;/P&gt;&lt;P&gt;b567&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; g&lt;/P&gt;&lt;P&gt;...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much in advance.&lt;/P&gt;</description>
    <pubDate>Fri, 24 Aug 2018 07:35:35 GMT</pubDate>
    <dc:creator>Li17</dc:creator>
    <dc:date>2018-08-24T07:35:35Z</dc:date>
    <item>
      <title>access information stored in macro variable to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/access-information-stored-in-macro-variable-to-create-new/m-p/489512#M127843</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a set of about 300 different macro variables each storing a list of product codes and I also have a dataset, listing all used product codes and their counts etc.. The codes in the 300 macro variables are only a part of all possible product codes and group the product codes into bigger categories.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to access the product code information stored in the macro variables to create a new variable in my dataset, which takes certain values depending on the specific codes stored in the different macro variables? I tried the following code but this does not work. Does anyone have an idea how I can do it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data b; set a ;&lt;BR /&gt;druggroup='M';&lt;BR /&gt;%macro groupcodes;&lt;BR /&gt;%do j=1 %to 300; %let e=%scan(&amp;amp;varlist3,&amp;amp;j); %if Bcode in (&amp;amp;&amp;amp;x&amp;amp;e) %then druggroup="&amp;amp;e";&lt;BR /&gt;run;&lt;BR /&gt;%end; %mend; %groupcodes&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I&amp;nbsp; would like to have in the end is a dataset&amp;nbsp; which looks like the exmple below:&lt;/P&gt;&lt;P&gt;bcode druggroup&lt;/P&gt;&lt;P&gt;b123&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a&lt;/P&gt;&lt;P&gt;b234&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; b&lt;/P&gt;&lt;P&gt;b122&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a&lt;/P&gt;&lt;P&gt;b129&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a&lt;/P&gt;&lt;P&gt;b255&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c&lt;/P&gt;&lt;P&gt;b567&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; g&lt;/P&gt;&lt;P&gt;...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much in advance.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Aug 2018 07:35:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/access-information-stored-in-macro-variable-to-create-new/m-p/489512#M127843</guid>
      <dc:creator>Li17</dc:creator>
      <dc:date>2018-08-24T07:35:35Z</dc:date>
    </item>
    <item>
      <title>Re: access information stored in macro variable to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/access-information-stored-in-macro-variable-to-create-new/m-p/489518#M127845</link>
      <description>&lt;P&gt;Please picture someone banging their head against the wall.&amp;nbsp; Macro is&amp;nbsp;&lt;U&gt;&lt;STRONG&gt;not&lt;/STRONG&gt;&lt;/U&gt; the place to be storing or processing data.&amp;nbsp; Data is stored in datasets - it even states this in the name.&amp;nbsp; You process data using Base SAS - this is the programming language.&amp;nbsp; Macro is nothing more than a text find/replace system, it does nothing itself but generate Base SAS code.&amp;nbsp; Macro is an advanced topic for very specific scenarios, almost all coding effort should be put into using Base SAS to do your tasks.&lt;/P&gt;
&lt;P&gt;Step 1, create a dataset which contain all the codes with a grouping variable, e.g.&lt;/P&gt;
&lt;P&gt;GROUP&amp;nbsp; &amp;nbsp;CODE&lt;/P&gt;
&lt;P&gt;a&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; b123&lt;/P&gt;
&lt;P&gt;a&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; b122&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;b&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; b234&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now you have a nice list of codes and groups which is easy to manipulate and use in other code.&lt;/P&gt;
&lt;P&gt;Step 2, merge the codes group onto your data based on code=code:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table want as 
  select  a.*,
          b.group
  from    have a
  left join codes b
  on      a.code=b.code;
quit;&lt;/PRE&gt;
&lt;P&gt;Voila.&amp;nbsp; Easy, simple, fast code to achieve what you need.&amp;nbsp; Simply put, use&amp;nbsp;&lt;U&gt;&lt;STRONG&gt;Base SAS programming&lt;/STRONG&gt;&lt;/U&gt; always, and only if there is some benefit, then add in some macro code.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Aug 2018 08:03:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/access-information-stored-in-macro-variable-to-create-new/m-p/489518#M127845</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-08-24T08:03:10Z</dc:date>
    </item>
    <item>
      <title>Re: access information stored in macro variable to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/access-information-stored-in-macro-variable-to-create-new/m-p/489521#M127847</link>
      <description>&lt;P&gt;Second what &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt; said. You are abusing the macro language for something it is not meant for.&lt;/P&gt;
&lt;P&gt;See Maxims 11, 14, 8, 7.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Go back to where you are defining those 300 macro variables, and store the values in a dataset. Or use the existing dataset, if you already have one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.youtube.com/watch?v=cRl-dmeLTT8" target="_blank"&gt;https://www.youtube.com/watch?v=cRl-dmeLTT8&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Aug 2018 08:30:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/access-information-stored-in-macro-variable-to-create-new/m-p/489521#M127847</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-08-24T08:30:24Z</dc:date>
    </item>
  </channel>
</rss>

