<?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: problem with sas code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387285#M92851</link>
    <description>&lt;P&gt;2 things, as mentioned before:&lt;/P&gt;
&lt;P&gt;- Post test data in the form of a datastep in the post using the {i} code window.&lt;/P&gt;
&lt;P&gt;- Macro is not needed. &amp;nbsp;From what I cans see here you doing a simple addition, so:&lt;/P&gt;
&lt;PRE&gt;data want;
  set loopcars;
  cyl=ifn(hp &amp;gt; 150,cyl=cyl+(10-cyl),cyl);
run;&lt;/PRE&gt;
&lt;P&gt;So if the condition is true, set cycl to add 10 up to 10, otherwise leave as is. &amp;nbsp;That is all thats needed (although I can't test as I will not download files and you haven't shown required output.&lt;/P&gt;
&lt;P&gt;Also, make the Subject line a bit moore descriptive.&lt;/P&gt;</description>
    <pubDate>Fri, 11 Aug 2017 10:39:28 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2017-08-11T10:39:28Z</dc:date>
    <item>
      <title>problem with sas code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387282#M92848</link>
      <description>&lt;P&gt;Hello guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am writing the below macro code to increase the number of cyl, such that the cars having hp &amp;gt; 150, cyl starts increasing till the time it gets equal to 10. Also, i have to reflect the magnitude by which the cyl has increased.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Problem: In each of the cyl value, 10 is getting added.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro do_loop (var1,var2);&lt;BR /&gt;%if &amp;amp;var1 &amp;gt; 150 %then&lt;BR /&gt;%do i=1%to 10%by 1;&lt;BR /&gt;&amp;amp;var2=&amp;amp;var2+1;&lt;BR /&gt;%if &amp;amp;var2=10 %then leave;%end;;&lt;BR /&gt;%mend do_loop;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data macro_iterloop;&lt;BR /&gt;set loopcars;&lt;BR /&gt;%do_loop(hp,cyl);&lt;BR /&gt;proc print data=macro_iterloop;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The dataset is attached for reference. Please help&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 10:32:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387282#M92848</guid>
      <dc:creator>atulsingh</dc:creator>
      <dc:date>2017-08-11T10:32:55Z</dc:date>
    </item>
    <item>
      <title>Re: problem with sas code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387285#M92851</link>
      <description>&lt;P&gt;2 things, as mentioned before:&lt;/P&gt;
&lt;P&gt;- Post test data in the form of a datastep in the post using the {i} code window.&lt;/P&gt;
&lt;P&gt;- Macro is not needed. &amp;nbsp;From what I cans see here you doing a simple addition, so:&lt;/P&gt;
&lt;PRE&gt;data want;
  set loopcars;
  cyl=ifn(hp &amp;gt; 150,cyl=cyl+(10-cyl),cyl);
run;&lt;/PRE&gt;
&lt;P&gt;So if the condition is true, set cycl to add 10 up to 10, otherwise leave as is. &amp;nbsp;That is all thats needed (although I can't test as I will not download files and you haven't shown required output.&lt;/P&gt;
&lt;P&gt;Also, make the Subject line a bit moore descriptive.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 10:39:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387285#M92851</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-08-11T10:39:28Z</dc:date>
    </item>
    <item>
      <title>Re: problem with sas code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387286#M92852</link>
      <description>&lt;P&gt;I want the code to be wriiten in macros&amp;nbsp;&lt;/P&gt;&lt;P&gt;as i have different sets of observations with same varibale and same task is to be done for all of them.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 10:41:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387286#M92852</guid>
      <dc:creator>atulsingh</dc:creator>
      <dc:date>2017-08-11T10:41:49Z</dc:date>
    </item>
    <item>
      <title>Re: problem with sas code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387288#M92854</link>
      <description>&lt;P&gt;Total misunderstanding of the macro preprocessor. The macro preprocessor is for creating dynamic code, &lt;EM&gt;&lt;U&gt;&lt;STRONG&gt;NOT&lt;/STRONG&gt;&lt;/U&gt;&lt;/EM&gt; for handling data!&lt;/P&gt;
&lt;P&gt;And there is a MUCH simpler solution than tediously counting up to a value:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
if hp &amp;gt; 150 then cyl = max(cyl,10);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 12:49:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387288#M92854</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-08-11T12:49:31Z</dc:date>
    </item>
    <item>
      <title>Re: problem with sas code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387289#M92855</link>
      <description>&lt;P&gt;Without examples how can I tell? &amp;nbsp;First write code which actually runs on one dataset, then look at how to change this to run on multiple datasets. &amp;nbsp;A quick change to my code:&lt;/P&gt;
&lt;PRE&gt;%macro A (ds=,var=,oth=);&lt;BR /&gt;data want;
  set &amp;amp;ds.;
  &amp;amp;var.=ifn($oth. &amp;gt; 150,&amp;amp;var.=&amp;amp;var.+(10-&amp;amp;var.),&amp;amp;var.);
run;&lt;BR /&gt;%mend A;&lt;BR /&gt;%A (ds=loopds,var=cyl,oth=hp);&lt;/PRE&gt;
&lt;P&gt;And it becomes a macro. &amp;nbsp;But I started with Base SAS - which is the programming language - then I looked to see how it would be repeated, not the other way of writing macro code to try to force things.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 10:45:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387289#M92855</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-08-11T10:45:13Z</dc:date>
    </item>
    <item>
      <title>Re: problem with sas code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387290#M92856</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138415"&gt;@atulsingh&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I want the code to be wriiten in macros&amp;nbsp;&lt;/P&gt;
&lt;P&gt;as i have different sets of observations with same varibale and same task is to be done for all of them.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;First of all, you DO NOT write "code" in macros. But you can make code dynamic for easier writing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro set_max(var1,var2);
if &amp;amp;var1 &amp;gt; 150 then &amp;amp;var2 = max(&amp;amp;var2,10);
%mend;

data want;
set have;
%set_max(hp,cyl)
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that no macro code is inside the macro, just data step code with dynamic variable names.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 10:45:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387290#M92856</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-08-11T10:45:44Z</dc:date>
    </item>
    <item>
      <title>Re: problem with sas code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387309#M92860</link>
      <description>&lt;P&gt;You can turn on MPRINT and MLOGIC to see what is happening. &amp;nbsp;Take a look at your macro, let's reformat it so that the macro logic steps are indented, but the SAS statements that it is generating are not.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro do_loop (var1,var2);
  %if &amp;amp;var1 &amp;gt; 150 %then %do i=1 %to 10 %by 1;
&amp;amp;var2=&amp;amp;var2+1;
    %if &amp;amp;var2=10 %then leave;
  %end; 
; 
%mend do_loop;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So now let's look at the call&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do_loop(hp,cyl)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And step through the macro logic and see what we expect it to do. &amp;nbsp;So if &amp;amp;VAR1 is replaced the hp and &amp;amp;VAR2 is replaced with cyl we get this body of the macro.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  %if hp&amp;gt; 150 %then %do i=1 %to 10 %by 1;
cyl=cyl+1;
    %if cyl=10 %then leave;
  %end;
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So let's check the logic. So the string "hp" is always greater than the string "150" so the %DO loop will run. And the string "cyl" is never equal to the string "10" so it never leaves early. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the macro will generate 10 assignment statements and one extra null statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As to your stated requirement:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;increase the number of cyl, such that the cars having hp &amp;gt; 150, cyl starts increasing till the time it gets equal to 10. Also, i have to reflect the magnitude by which the cyl has increased&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;To find the number of cylinders to add you will need some subtraction.&amp;nbsp;To test value of HP you will need an IF statement.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if hp &amp;gt; 150 and cyl &amp;gt; 10 then added_cyl = 10-cyl;
else added_cyl=0;
cyl=cyl+added_cyl;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Not sure how you could change this into a macro. What is it that varies? &amp;nbsp;Your example assumed it was the variable names, but to me it makes more sense that the values 150 and 10 would be the values that vary.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro add_cyl(hp_cutoff,max_cyl);
if hp &amp;gt; &amp;amp;hp_cutoff and cyl &amp;gt; &amp;amp;max_cyl then added_cyl = &amp;amp;max_cyl-cyl;
else added_cyl=0;
cyl=cyl+added_cyl;
%mend add_cyl;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 12:39:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387309#M92860</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-08-11T12:39:05Z</dc:date>
    </item>
    <item>
      <title>Re: problem with sas code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387314#M92862</link>
      <description>how the hp string will always be greater than 150. I think it will check first whether it's greater than 150 or smaller</description>
      <pubDate>Fri, 11 Aug 2017 12:53:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387314#M92862</guid>
      <dc:creator>atulsingh</dc:creator>
      <dc:date>2017-08-11T12:53:46Z</dc:date>
    </item>
    <item>
      <title>Re: problem with sas code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387318#M92865</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138415"&gt;@atulsingh&lt;/a&gt; wrote:&lt;BR /&gt;how the hp string will always be greater than 150. I think it will check first whether it's greater than 150 or smaller&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In the macro statement&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if hp &amp;gt; 150 %then ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The letters hp are just that, the letters hp. &amp;nbsp;They have nothing to do with any dataset variable that might be named using the same string of letters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A lowercase H is always greater than the digit 1 in the ASCII coding system that SAS uses to store characters so hp is always greater than 150 in the lexigraphical ordering that SAS uses to compare strings.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 13:03:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387318#M92865</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-08-11T13:03:49Z</dc:date>
    </item>
    <item>
      <title>Re: problem with sas code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387321#M92868</link>
      <description>&lt;P&gt;So how to refer hp to the 'hp variable in the csv dataset?&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 13:15:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387321#M92868</guid>
      <dc:creator>atulsingh</dc:creator>
      <dc:date>2017-08-11T13:15:52Z</dc:date>
    </item>
    <item>
      <title>Re: problem with sas code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387324#M92869</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138415"&gt;@atulsingh&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;So how to refer hp to the 'hp variable in the csv dataset?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Use normal SAS statements like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ... ;
  set ... .;
if hp &amp;gt; 150 then ...
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The SAS macro processor is for generating SAS code.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 13:22:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387324#M92869</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-08-11T13:22:34Z</dc:date>
    </item>
    <item>
      <title>Re: problem with sas code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387384#M92888</link>
      <description>&lt;HR /&gt;
&lt;P&gt;atulsingh wrote:&lt;BR /&gt;how the hp string will always be greater than 150. I think it will check first whether it's greater than 150 or smaller&lt;/P&gt;
&lt;HR /&gt;
&lt;P&gt;You still suffer from a misunderstanding of what the macro processor is, what it does and when it does it.&lt;/P&gt;
&lt;P&gt;The macro processor is invoked when a macro trigger (% or &amp;amp;) is encountered in the code while the code is read. It then produces text which is fed back to the "main" SAS interpreter for interpretation (or compilation in case of a data step). Since it does its work long before a data step runs, it has never access to the values present in the step.&lt;/P&gt;
&lt;P&gt;Another feature of the macro processor is that it deals only with text; it has no datatype numeric. Your condition, translated to data step code, is therefore equivalent to&lt;/P&gt;
&lt;P&gt;if "hp" &amp;gt; "150"&lt;/P&gt;
&lt;P&gt;which is always true.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 15:34:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/problem-with-sas-code/m-p/387384#M92888</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-08-11T15:34:53Z</dc:date>
    </item>
  </channel>
</rss>

