<?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 substr, IN on array elements in MACRO in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-substr-IN-on-array-elements-in-MACRO/m-p/300778#M312300</link>
    <description>&lt;P&gt;If you have many variables in multiple datasets that need similar RECODING then possibly you want a custom INFORMAT.&lt;/P&gt;
&lt;P&gt;Please see the example below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format library=work;
invalue YNM (upcase)
'YES','NO'=1
other = 0;
run;

data example;
   input x $;
datalines;
yes
Yes
No
no
maybe
.
never
;
run;

data want;
   set example;
   varx = input(x,YNM.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that if this were my data I would be very tempted to read the values initially with the appropriate informat instead recoding them later.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Informats and Formats can sometimes remove a lot of IF/THEN/ELSE code involving non-overlapping values for single variables.&lt;/P&gt;
&lt;P&gt;Also note the inclusion of "never" in the data example. Your code using substr would set this to 1 but the original value was not "no".&lt;/P&gt;</description>
    <pubDate>Mon, 26 Sep 2016 15:16:47 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2016-09-26T15:16:47Z</dc:date>
    <item>
      <title>Using substr, IN on array elements in MACRO</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-substr-IN-on-array-elements-in-MACRO/m-p/300595#M312297</link>
      <description>&lt;P&gt;Hello SAS community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've built some code in data steps that I am now going to have to repeat on many other data sources, so I'm changing the code over to %macro in order to easily apply it many times. However I've run into an issue.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First i will show the original code snippet, then I will show a toy example I am playing with to find out exactly what is causing the problem:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do j=1 %to &amp;amp;d;
  %if %substr(array{j},1,5) in ("str1", "str2", "str3") %then %do;
      if var1 &amp;gt; 3 then var2="no";
  %end;
%end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;THis produces error4:&lt;/P&gt;&lt;P&gt;"Macro function %substr has too many arguments. The excess arguments will be ignored.&lt;/P&gt;&lt;P&gt;A character operand was found in the %EVAL function or %IF condition where a number operand is required. The condition was: j}&lt;/P&gt;&lt;P&gt;Argument 2 to macro function %substr is not a number.&lt;/P&gt;&lt;P&gt;Operand missing for IN operator to argument to %EVAL function"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So one of the first things I realised when converting code to macros is that the %dos and %ifs need to have percent prefix to avoid issues, then i found out that substr() is not valid in macros, so you ahve to use either %substr or %sysfunc(substr()). Then I learned that "IN" is also not valid, unless you include options to allow it before code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;GIven these lessons, I have tried to now use a toy example just to test out the general functionality of declaring array elements and checking substr of the value of them using "in" operand. Here's my test code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input id $ answer1 $ answer2 $;
datalines;
1 yes maybe 
2 no maybe
3 maybe maybe
;
run;

options minoperator mindelimiter=',';
%macro substringtest();

data test2;
set test;
array answer{2};

%do i=1 %to 2;
%if %substr(answer{i},1,1) in ("y", "n") %then %do;
certain&amp;amp;i=1;
%end;
%else %do;
certain&amp;amp;i=0;
%end;
%end;

run;
%mend;

%substringtest;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;When running this, I get no mlogic issues, but the values for obs 1 and 2 "certain1" should be "1", but they are all 0 for certain1 and certain2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm now a little confused regarding a few things&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) Why is the substr argument workign in test code but no the real code.&lt;/P&gt;&lt;P&gt;2) Why isn't the test code producing proper variable outputs reflecting correct logic.&lt;/P&gt;&lt;P&gt;3) Is there some easier way to get around using do blocks? I have other macros I converted to which uses substr and arrays and in just fine, but because i have further conditions after, i have some parts that are in "do" blocks. Maybe there's a workaround to just not do this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks ahead of time.&lt;/P&gt;</description>
      <pubDate>Sun, 25 Sep 2016 08:08:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-substr-IN-on-array-elements-in-MACRO/m-p/300595#M312297</guid>
      <dc:creator>chrisengel</dc:creator>
      <dc:date>2016-09-25T08:08:09Z</dc:date>
    </item>
    <item>
      <title>Re: Using substr, IN on array elements in MACRO</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-substr-IN-on-array-elements-in-MACRO/m-p/300599#M312298</link>
      <description>&lt;P&gt;There's nothing in your loop that requires a macro. If you're in a data step use a regular do loop.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use a macro do loop to repeat procedures.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code below would change to declare a certain array as well.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this doesn't reflect your actual situation then please make an example that's relevant.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. macro comparisons don't include quotes. in y, n&amp;nbsp;&lt;A href="http://support.sas.com/kb/35/591.html" target="_blank"&gt;http://support.sas.com/kb/35/591.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;2. Your evaluation is never hitting true. Add %put to,each %if condition so you can trace your logic.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. See stuff before this.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 25 Sep 2016 10:36:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-substr-IN-on-array-elements-in-MACRO/m-p/300599#M312298</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-09-25T10:36:07Z</dc:date>
    </item>
    <item>
      <title>Re: Using substr, IN on array elements in MACRO</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-substr-IN-on-array-elements-in-MACRO/m-p/300604#M312299</link>
      <description>&lt;P&gt;If you're using DATA step code, arrays might look like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;array answer {2};&lt;/P&gt;
&lt;P&gt;array certain {2};&lt;/P&gt;
&lt;P&gt;do i=1 to dim(answer);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if substr(answer{i}, 1, 1) in ('n', 'y') then certain{i}=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;else certain{i}=0;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Even that could likely be simplified by removing SUBSTR entirely. &amp;nbsp;It all depends on what might be found in the ANSWER variables. &amp;nbsp;If macro language were to be inserted into this program, it would only be to define the array dimensions: &amp;nbsp;{&amp;amp;d}&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you to use macro language instead of arrays, it might look like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%do i=1 %to 2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if substr(answer&amp;amp;i 1, 1) in ('n', 'y') then certain&amp;amp;i=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;else certain&amp;amp;i=0;&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro language is not processing your data. &amp;nbsp;It is generating the proper SAS statements needed to process your data once the DATA step begins to execute.&lt;/P&gt;</description>
      <pubDate>Sun, 25 Sep 2016 11:20:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-substr-IN-on-array-elements-in-MACRO/m-p/300604#M312299</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-09-25T11:20:03Z</dc:date>
    </item>
    <item>
      <title>Re: Using substr, IN on array elements in MACRO</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-substr-IN-on-array-elements-in-MACRO/m-p/300778#M312300</link>
      <description>&lt;P&gt;If you have many variables in multiple datasets that need similar RECODING then possibly you want a custom INFORMAT.&lt;/P&gt;
&lt;P&gt;Please see the example below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format library=work;
invalue YNM (upcase)
'YES','NO'=1
other = 0;
run;

data example;
   input x $;
datalines;
yes
Yes
No
no
maybe
.
never
;
run;

data want;
   set example;
   varx = input(x,YNM.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that if this were my data I would be very tempted to read the values initially with the appropriate informat instead recoding them later.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Informats and Formats can sometimes remove a lot of IF/THEN/ELSE code involving non-overlapping values for single variables.&lt;/P&gt;
&lt;P&gt;Also note the inclusion of "never" in the data example. Your code using substr would set this to 1 but the original value was not "no".&lt;/P&gt;</description>
      <pubDate>Mon, 26 Sep 2016 15:16:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-substr-IN-on-array-elements-in-MACRO/m-p/300778#M312300</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-09-26T15:16:47Z</dc:date>
    </item>
  </channel>
</rss>

