<?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 the year(variable) function in  %IF%THEN%ELSE in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-the-year-variable-function-in-IF-THEN-ELSE/m-p/480970#M286703</link>
    <description>Thank you for the reply.</description>
    <pubDate>Tue, 24 Jul 2018 23:50:41 GMT</pubDate>
    <dc:creator>mjabed600</dc:creator>
    <dc:date>2018-07-24T23:50:41Z</dc:date>
    <item>
      <title>Using the year(variable) function in  %IF%THEN%ELSE</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-the-year-variable-function-in-IF-THEN-ELSE/m-p/480955#M286698</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I have recently started learning SAS was trying to create a macro but ran into an error. I have a dataset with a variable order_date which is a numeric type in mmddyy10 format and order_type which is numeric as well with values of either 1 or 2. I am trying to write a macro where, I can calculate the frequency of orders of a specific type of order in a specific year. OR lets say I only specify a year, i should get frequency of all the types of orders from that year. Also, wanted to have appropriate titles for example,&amp;nbsp;"total number of orders for 2005" or "total number of type 1 orders in 2005". I am using SAS university edition. below is the code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro frequency(orderyr=, type= );&lt;BR /&gt;%if year(order_date)= &amp;amp;orderyr and order_type= &amp;amp;type %then&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; title"Total Type &amp;amp;type orders for &amp;amp;orderyr";&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; proc freq data=myexcel;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; tables order_type * Quantity/norow nocol nocum nopercent;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; %end;&lt;BR /&gt;&lt;BR /&gt;%else %if year(order_date) = &amp;amp;orderyr %then&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;%do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;title"Total Orders for all types in &amp;amp;orderyr";&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;proc freq data=myexcel;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;tables order_type * quantity/norow nocol nocum nopercent;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; %end;&lt;BR /&gt;%mend frequency;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%frequency;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this is the error :&amp;nbsp;&lt;/P&gt;&lt;PRE class="sasLog"&gt;ERROR: Required operator not found in expression: year(order_date)= &amp;amp;orderyr and order_type= &amp;amp;type&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;ANy help is really appreciated. Thank you&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jul 2018 22:21:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-the-year-variable-function-in-IF-THEN-ELSE/m-p/480955#M286698</guid>
      <dc:creator>mjabed600</dc:creator>
      <dc:date>2018-07-24T22:21:43Z</dc:date>
    </item>
    <item>
      <title>Re: Using the year(variable) function in  %IF%THEN%ELSE</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-the-year-variable-function-in-IF-THEN-ELSE/m-p/480959#M286700</link>
      <description>&lt;P&gt;There is nothing about your program that brings in ORDER_DATE.&amp;nbsp; Is it supposed to be part of the SAS data set MYEXCEL?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In that case you might begin your macro with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;/P&gt;
&lt;P&gt;set myexcel;&lt;/P&gt;
&lt;P&gt;call symputx('order_year', year(order_date));&lt;/P&gt;
&lt;P&gt;stop;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then you could compare with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%if &amp;amp;order_year = &amp;amp;orderyr and .......&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jul 2018 22:39:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-the-year-variable-function-in-IF-THEN-ELSE/m-p/480959#M286700</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-07-24T22:39:01Z</dc:date>
    </item>
    <item>
      <title>Re: Using the year(variable) function in  %IF%THEN%ELSE</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-the-year-variable-function-in-IF-THEN-ELSE/m-p/480960#M286701</link>
      <description>&lt;P&gt;Your first issue is that your call to the macro does not set values for either of orderyr or type macro variables. The call should look something like:&lt;/P&gt;
&lt;P&gt;%frequency(2018, 1);&lt;/P&gt;
&lt;P&gt;if you want to use 2018 for the orderyr and 1 for the type. When you leave them blank the %if doesn't have anything to use and you have to use some additional code (not obvious) to test whether you supplied a value at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro language doesn't actually use dataset variable values, at least not without a great deal of complexity.&lt;/P&gt;
&lt;P&gt;You can reference the value of a macro variable though.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might consider&amp;nbsp;something like&lt;/P&gt;
&lt;PRE&gt;proc freq data=myexcel;
   where year(order_date)=&amp;amp;orderyr. and order_type=&amp;amp;type.; 
   tables order_type * Quantity/norow nocol nocum nopercent;
run;&lt;/PRE&gt;
&lt;P&gt;You would be better off to set a special value for the macro variables that indicates they are not actually used for the report, then you have something actually test. I would suggest a value unlikely to appear such as _donotuse_&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps something like this for two of the four cases involved:&lt;/P&gt;
&lt;PRE&gt;%if &amp;amp;orderyr. = _donotuse_ and &amp;amp;type.=_donotuse_ %then %do;
   title"Total Orders for all types and all years";
   proc freq data=myexcel;
      tables order_type * Quantity/norow nocol nocum nopercent;
   run;
%end;
%else %if &amp;amp;orderyr. = _donotuse_ and &amp;amp;type. ne _donotuse_ %then %do;
   title"Total Type &amp;amp;type orders  and all years";
   proc freq data=myexcel;
      where order_type=&amp;amp;type.;
      tables order_type * Quantity/norow nocol nocum nopercent;
   run;
%end;&lt;/PRE&gt;
&lt;P&gt;Other two cases left as an exercise.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro call would might then look like %frequency(_donotuse_, 1);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since macro programming only generates code the appropriate approach is to get a Procedure or data step working without any macro code that generates what you want. Then look at how to specify the elements used from the macro language. In this case the key element is 1) selecting&amp;nbsp; the records to analyze, which would be a where statement since you want to do only one value of one or two variables and 2) change the title.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jul 2018 22:52:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-the-year-variable-function-in-IF-THEN-ELSE/m-p/480960#M286701</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-07-24T22:52:37Z</dc:date>
    </item>
    <item>
      <title>Re: Using the year(variable) function in  %IF%THEN%ELSE</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-the-year-variable-function-in-IF-THEN-ELSE/m-p/480969#M286702</link>
      <description>thank you for your response. You're right, I tried to write a macro before trying to create a working proc step. Thank you once again</description>
      <pubDate>Tue, 24 Jul 2018 23:49:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-the-year-variable-function-in-IF-THEN-ELSE/m-p/480969#M286702</guid>
      <dc:creator>mjabed600</dc:creator>
      <dc:date>2018-07-24T23:49:21Z</dc:date>
    </item>
    <item>
      <title>Re: Using the year(variable) function in  %IF%THEN%ELSE</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-the-year-variable-function-in-IF-THEN-ELSE/m-p/480970#M286703</link>
      <description>Thank you for the reply.</description>
      <pubDate>Tue, 24 Jul 2018 23:50:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-the-year-variable-function-in-IF-THEN-ELSE/m-p/480970#M286703</guid>
      <dc:creator>mjabed600</dc:creator>
      <dc:date>2018-07-24T23:50:41Z</dc:date>
    </item>
  </channel>
</rss>

