<?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: Using scan( ) to pull macro string in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-scan-to-pull-macro-string/m-p/451757#M283826</link>
    <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let factor1 = 'a' 'b' 'c';
%let factor2 = 'd' 'e' 'f';

data WANT;
  VAR='e'; 
  %macro loop;
    %local i;
    %do i=1 %to 2;
      FACTOR&amp;amp;i. = ( VAR in (&amp;amp;&amp;amp;factor&amp;amp;i.) );
    %end;
  %mend;
  %loop
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;TABLE width="259"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="117"&gt;VAR&lt;/TD&gt;
&lt;TD width="71"&gt;FACTOR1&lt;/TD&gt;
&lt;TD width="71"&gt;FACTOR2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;e&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 06 Apr 2018 03:55:07 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2018-04-06T03:55:07Z</dc:date>
    <item>
      <title>Using scan( ) to pull macro string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-scan-to-pull-macro-string/m-p/451720#M283825</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset where I am trying to categorize values of a variable into groups.&lt;/P&gt;&lt;P&gt;Say I want to categorize as so:&lt;/P&gt;&lt;P&gt;a, b, or c = factor1&lt;/P&gt;&lt;P&gt;d, e, or f = factor2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following codes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let factor1 = %str("a", "b", "c");&lt;/P&gt;&lt;P&gt;%let factor2 = %str("d", "e", "f");&lt;/P&gt;&lt;P&gt;etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(say I have around 30 of these different factors)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I grouped all these macros into one big list:&lt;/P&gt;&lt;P&gt;%let allfactors = &amp;amp;factor1 &amp;amp;factor2 &amp;amp;factor3 etc&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also have a list with all the categories I want:&lt;/P&gt;&lt;P&gt;%let list = factor1 factor2 factor 3 etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I create a code where I scan the place of the &amp;amp;factor and categorize the observation's value according to the place of the &amp;amp;list?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example,&lt;/P&gt;&lt;P&gt;If an observation has value C, I want to categorize this as FACTOR1 = 1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried doing this:&lt;/P&gt;&lt;P&gt;%macro test(data=);&lt;BR /&gt;data &amp;amp;data._o;&lt;BR /&gt;set &amp;amp;data.;&lt;BR /&gt;%do i=1 %to %sysfunc(countw(&amp;amp;list));&lt;BR /&gt;if variable&amp;nbsp;in: (%scan(&amp;amp;allfactors, &amp;amp;i)) then %scan(&amp;amp;list, &amp;amp;i)=1;&lt;BR /&gt;%end;&lt;BR /&gt;%mend test;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%test(data=testdata);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I run this code, the entire macro for ALLFACTORS is considered. For example, the &amp;amp;i includes only value 'a' but I want the value of &amp;amp;i to be according to the&amp;nbsp;&amp;amp;list&amp;nbsp;placement (e.g. i=1 should be factor1 in LIST and &amp;amp;factor1 in ALLFACTORS.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How should I change my code?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you, and apologies in advance if this is confusing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 23:54:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-scan-to-pull-macro-string/m-p/451720#M283825</guid>
      <dc:creator>soomx</dc:creator>
      <dc:date>2018-04-05T23:54:58Z</dc:date>
    </item>
    <item>
      <title>Re: Using scan( ) to pull macro string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-scan-to-pull-macro-string/m-p/451757#M283826</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let factor1 = 'a' 'b' 'c';
%let factor2 = 'd' 'e' 'f';

data WANT;
  VAR='e'; 
  %macro loop;
    %local i;
    %do i=1 %to 2;
      FACTOR&amp;amp;i. = ( VAR in (&amp;amp;&amp;amp;factor&amp;amp;i.) );
    %end;
  %mend;
  %loop
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;TABLE width="259"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="117"&gt;VAR&lt;/TD&gt;
&lt;TD width="71"&gt;FACTOR1&lt;/TD&gt;
&lt;TD width="71"&gt;FACTOR2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;e&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Apr 2018 03:55:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-scan-to-pull-macro-string/m-p/451757#M283826</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-04-06T03:55:07Z</dc:date>
    </item>
    <item>
      <title>Re: Using scan( ) to pull macro string</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-scan-to-pull-macro-string/m-p/451758#M283827</link>
      <description>&lt;P&gt;Sample data would help a lot in explaining your problem.&amp;nbsp; Also having a working SAS program before trying to figure out how to generate it with macro logic would help a lot.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sounds like you want to place the list of values into separate macro variables and then also have a macro variable that lists the macro variables? In that case list the NAMES of the macro variables, not their values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let color='red' 'green';
%let style='modern' 'classic';
%let allfactors = color style ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And the code you want to generate is essentially&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;color = MYVAR in ('red' 'green');
style = MYVAR in ('modern' 'classic');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So your %DO loop should look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%local i factor ;
%do i=1 %to %sysfunc(countw(&amp;amp;allfactors));
 %let factor=%scan(&amp;amp;allfactors,&amp;amp;i);
 &amp;amp;factor = MYVAR in (&amp;amp;&amp;amp;&amp;amp;factor) ;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;If you just want to name the factors FACTOR1, FACTOR2, etc then you don't need the list. Instead you just need to know how many factors there are.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%local i;
%do i=1 %to &amp;amp;n_factors ;
 factor&amp;amp;i = MYVAR in (&amp;amp;&amp;amp;factor&amp;amp;i) ;
%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Apr 2018 04:31:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-scan-to-pull-macro-string/m-p/451758#M283827</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-04-06T04:31:04Z</dc:date>
    </item>
  </channel>
</rss>

