<?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: Help with Macro if then else into Case? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-if-then-else-into-Case/m-p/319968#M70385</link>
    <description>&lt;P&gt;Well that is the problem, I don't know if it is even posible for starters and I have not yet seen one like I am hoping to do not inside a Proc SQL statments.&lt;BR /&gt;&lt;BR /&gt;Here is my sample miltiple part "if then else" that works, that I would love to see a case or swith for:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro date_check;
 %global snapshot_date2;
%if &amp;amp;snapshot2=midterm %then 
	%do;
		%let snapshot_date2=&amp;amp;midterm_date.;
		%put &amp;amp;midterm_date.| " midterm_date " ;
	%end;

%else %if &amp;amp;snapshot2=census %then 
	%do;
		%let snapshot_date2=&amp;amp;census_date.;
		%put &amp;amp;census_date.| " census_date " ;
	%end;

%else %if &amp;amp;snapshot2=ceneot %then 
	%do;
		%let snapshot_date2=&amp;amp;term_end_snapshot_dt.;
		%put &amp;amp;term_end_snapshot_dt.| " term_end " ;
	%end;

%else %if &amp;amp;snapshot2=eot %then 
	%do;
		%let snapshot_date2=&amp;amp;term_end_snapshot_dt.;
		%put &amp;amp;term_end_snapshot_dt.| " term_end " ;
	%end;

%else %if &amp;amp;snapshot2=begin %then 
	%do;
		%let snapshot_date2=&amp;amp;term_begin_dt.;
		%put &amp;amp;term_begin_dt.| " term_begin " ;
	%end;

 /* etc. */

%else 
	%do; /* default */
		%let snapshot_date2=&amp;amp;census_date.;
		%put &amp;amp;census_date.| " census " ;
	%end;
%put &amp;amp;snapshot_date2. &amp;amp;snapshot2;
%mend date_check;
%date_check;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I assume I would just change the macro to take one parm, the 'snapshot2' var, however after that I am not sure what to do.&amp;nbsp;&amp;nbsp; The dates examples are not that big of a deal for the formatting, any 4 or 5 'timedate22.' assigned to these vars will do.&lt;/P&gt;</description>
    <pubDate>Wed, 21 Dec 2016 16:08:32 GMT</pubDate>
    <dc:creator>kjohnsonm</dc:creator>
    <dc:date>2016-12-21T16:08:32Z</dc:date>
    <item>
      <title>Help with Macro if then else into Case?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-if-then-else-into-Case/m-p/319395#M70141</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;
&lt;P&gt;I have a basic macro if then else, I need to convert into a case statement.&amp;nbsp;&amp;nbsp; Can someone point me to a good reference please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In my case I am just going out for a given term and snapshot and getting the correct date for use in a single macro var for later use in soft coding my programs.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* So I get my temp dates, out of one record for the term I want,…;

* --- get census date ---;
proc sql noprint;
select term_census_dt into: census_date
from My_DB.xw_term
where strm="&amp;amp;strm.";
quit;
%put &amp;amp;census_date.;

* --- get midterm date ---;
proc sql noprint;
select term_midterm_dt into: midterm_date
from My_DB.xw_term
where strm="&amp;amp;strm.";
quit;
%put &amp;amp;midterm_date.;

* --- get term_end date ---;
proc sql noprint;
select term_end_dt into: term_end_dt
from My_DB.xw_term
where strm="&amp;amp;strm.";
quit;
%put &amp;amp;term_end_dt.;

*…etc …;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt; &lt;BR /&gt; * Next I need to pass one of (census, midterm,eot,ceneot) to my macro and get the correct date into my var.l&lt;BR /&gt; * for program to unify the code to use the same variable everywhere;&lt;/P&gt;
&lt;P&gt;* old code;&lt;BR /&gt; /*%let snapshot_date2=&amp;amp;census_date.;*/&lt;BR /&gt; &lt;/P&gt;
&lt;P&gt;* current macro, that needs to be a case/switch;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; %macro date_check;
 %global snapshot_date2;
%if &amp;amp;snapshot2=census %then
    %do;
        %let snapshot_date2=&amp;amp;census_date.;
        %put &amp;amp;snapshot_date2.;
    %end;
%else
    %do;
        %let snapshot_date2=&amp;amp;midterm_date.;
        %put &amp;amp;snapshot_date2.;
    %end;
%mend date_check;
%date_check;

%put &amp;amp;strm.;
%put &amp;amp;snapshot.;
%put &amp;amp;snapshot2.;
%put &amp;amp;snapshot_date2.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;I was trying to modify some code I found on this forum but am failing to get it to work, thus I just thought a fresh start with where I am and what I wanted to do could be a good reboot.&amp;nbsp; TIA -KJ&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Dec 2016 21:17:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-if-then-else-into-Case/m-p/319395#M70141</guid>
      <dc:creator>kjohnsonm</dc:creator>
      <dc:date>2016-12-15T21:17:59Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Macro if then else into Case?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-if-then-else-into-Case/m-p/319401#M70143</link>
      <description>&lt;P&gt;Well I would still like to know how to make a macro case statment work, however I have a solution that shortens my line count and is very readable.&amp;nbsp;&amp;nbsp;&amp;nbsp; with a Proc SQL and into and case all at once, in stead of 4 proc sqls and a macro :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options nocenter;
options ls=256;

libname My_DB odbc dsn=My_DB schema = dbo;

%let prog_suffix=;
%let and_snapshot=;
%let strm=2167;

%let snapshot='midterm';
%let snapshot2=midterm;

%let census_date=;
%let midterm_date=;
%let term_end_dt=;
%let snapshot_date2=;

proc sql ;
select 
&amp;amp;snapshot. as snapshot
,term_census_dt ,term_midterm_dt ,term_end_dt 
,case when calculated snapshot = 'midterm' then term_midterm_dt 
     when calculated snapshot = 'census' then  term_census_dt
	 /* etc add other snapshots here */
	 else term_census_dt end as snapshot_date2 format DATETIME22.3
into :census_date, :midterm_date, :term_end_dt, :snapshot_date2
from My_DB.xw_term
where strm="&amp;amp;strm.";
quit;
%put &amp;amp;census_date.;
%put &amp;amp;midterm_date.;
%put &amp;amp;term_end_dt.;
%put &amp;amp;snapshot_date2.;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output from my put statments like I wanted for later use as a macro var as needed:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I did not know you could use into for more than one field at a time, and I figured if i can get them all at once why not try a case statement in the Proc SQL at the sametime.&amp;nbsp;&amp;nbsp; cheers.&amp;nbsp; -KJ&lt;BR /&gt;snapshot&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; term_census_dt&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; term_midterm_dt&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; term_end_dt&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; snapshot_date2&lt;BR /&gt;ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ&lt;BR /&gt;midterm&amp;nbsp;&amp;nbsp; 02SEP2016:00:00:00.000&amp;nbsp; 12OCT2016:00:00:00.000&amp;nbsp; 16DEC2016:00:00:00.000&amp;nbsp; 12OCT2016:00:00:00.000&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Dec 2016 23:05:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-if-then-else-into-Case/m-p/319401#M70143</guid>
      <dc:creator>kjohnsonm</dc:creator>
      <dc:date>2016-12-15T23:05:10Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Macro if then else into Case?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-if-then-else-into-Case/m-p/319413#M70145</link>
      <description>&lt;P&gt;Like this (2 solutions shown: one in proc sql and one outside)&amp;nbsp; ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro test(snapshot,strm);

  %global census_date midterm_date term_end_dt snapshot_date2;

  proc sql noprint;
    select TERM_CENSUS_DT 
          ,TERM_MIDTERM_DT 
          ,TERM_END_DT 
          ,%if &amp;amp;snapshot. = midterm %then TERM_MIDTERM_DT; 
           %if &amp;amp;snapshot. = census  %then TERM_CENSUS_DT ;
           %else                          TERM_CENSUS_DT ; as SNAPSHOT_DATE2 format datetime22.3
      into :census_date, :midterm_date, :term_end_dt, :snapshot_date2
    from MY_DB.XW_TERM
    where STRM="&amp;amp;strm.";
  quit;

%mend;

%macro test(snapshot,strm);

  %global census_date midterm_date term_end_dt snapshot_date2;

  proc sql noprint;
    select TERM_CENSUS_DT 
          ,TERM_MIDTERM_DT 
          ,TERM_END_DT 
      into :census_date, :midterm_date, :term_end_dt
    from MY_DB.XW_TERM
    where STRM="&amp;amp;strm.";
  quit;

  %if &amp;amp;snapshot. = midterm %then %let snapshot_date2=&amp;amp;term_midterm_dt; 
  %if &amp;amp;snapshot. = census  %then %let snapshot_date2=&amp;amp;term_census_dt ;
  %else                          %let snapshot_date2=&amp;amp;term_census_dt ; 

%mend;


%test(midterm,2167);

%put &amp;amp;=census_date;
%put &amp;amp;=midterm_date;
%put &amp;amp;=term_end_dt;
%put &amp;amp;=snapshot_date2;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 16 Dec 2016 00:30:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-if-then-else-into-Case/m-p/319413#M70145</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2016-12-16T00:30:15Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Macro if then else into Case?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-if-then-else-into-Case/m-p/319492#M70186</link>
      <description>&lt;P&gt;Why do you need all the macro code? &amp;nbsp;One of SQL's biggest powers is the ability to both join data and to sub-query. &amp;nbsp;Using these techniques you can easily add your date into your data without having all the code:&lt;/P&gt;
&lt;PRE&gt;%let term=midterm;

proc sql;
  create table WANT as
  select  *,
          (select TERM_&amp;amp;term._DT from MY_DB.XW_TERM where STRM="&amp;amp;term.") as DATE_I_WANT
  from    HAVE;
quit;&lt;/PRE&gt;
&lt;P&gt;Note, I am only guessing what your doing (no test data/required output) to do a proper evaluation.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 10:30:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-if-then-else-into-Case/m-p/319492#M70186</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-12-16T10:30:48Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Macro if then else into Case?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-if-then-else-into-Case/m-p/319572#M70231</link>
      <description>&lt;P&gt;lol,&amp;nbsp;&amp;nbsp;&amp;nbsp; "unlimited power"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 16:01:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-if-then-else-into-Case/m-p/319572#M70231</guid>
      <dc:creator>kjohnsonm</dc:creator>
      <dc:date>2016-12-16T16:01:35Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Macro if then else into Case?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-if-then-else-into-Case/m-p/319577#M70235</link>
      <description>&lt;P&gt;Well in short if I can do a "if then else in a macro" I want to know how to do a "case", for three or more choice options it is as simple as that. I cannot seem to find an example that is a straight forward change of the original macro to a case macro.&amp;nbsp;&amp;nbsp; What I am really doing is automating the way my programs startup and get their parameters automatically from a few crosswalk tables that are very stable and extremely accurate to help control the way the rest of my programs operate.&amp;nbsp; But in some cases I cannot "lookup" the data I have to just assign it from a list of options. -KJ&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 16:10:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-if-then-else-into-Case/m-p/319577#M70235</guid>
      <dc:creator>kjohnsonm</dc:creator>
      <dc:date>2016-12-16T16:10:50Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Macro if then else into Case?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-if-then-else-into-Case/m-p/319661#M70265</link>
      <description>&lt;P&gt;Can you show what the actual BASE SAS code is supposed to be? If you don't know what the "case statement" would look like as part of a Proc SQL step then it's a bit hard to get there.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 21:11:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-if-then-else-into-Case/m-p/319661#M70265</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-12-16T21:11:14Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Macro if then else into Case?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-if-then-else-into-Case/m-p/319968#M70385</link>
      <description>&lt;P&gt;Well that is the problem, I don't know if it is even posible for starters and I have not yet seen one like I am hoping to do not inside a Proc SQL statments.&lt;BR /&gt;&lt;BR /&gt;Here is my sample miltiple part "if then else" that works, that I would love to see a case or swith for:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro date_check;
 %global snapshot_date2;
%if &amp;amp;snapshot2=midterm %then 
	%do;
		%let snapshot_date2=&amp;amp;midterm_date.;
		%put &amp;amp;midterm_date.| " midterm_date " ;
	%end;

%else %if &amp;amp;snapshot2=census %then 
	%do;
		%let snapshot_date2=&amp;amp;census_date.;
		%put &amp;amp;census_date.| " census_date " ;
	%end;

%else %if &amp;amp;snapshot2=ceneot %then 
	%do;
		%let snapshot_date2=&amp;amp;term_end_snapshot_dt.;
		%put &amp;amp;term_end_snapshot_dt.| " term_end " ;
	%end;

%else %if &amp;amp;snapshot2=eot %then 
	%do;
		%let snapshot_date2=&amp;amp;term_end_snapshot_dt.;
		%put &amp;amp;term_end_snapshot_dt.| " term_end " ;
	%end;

%else %if &amp;amp;snapshot2=begin %then 
	%do;
		%let snapshot_date2=&amp;amp;term_begin_dt.;
		%put &amp;amp;term_begin_dt.| " term_begin " ;
	%end;

 /* etc. */

%else 
	%do; /* default */
		%let snapshot_date2=&amp;amp;census_date.;
		%put &amp;amp;census_date.| " census " ;
	%end;
%put &amp;amp;snapshot_date2. &amp;amp;snapshot2;
%mend date_check;
%date_check;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I assume I would just change the macro to take one parm, the 'snapshot2' var, however after that I am not sure what to do.&amp;nbsp;&amp;nbsp; The dates examples are not that big of a deal for the formatting, any 4 or 5 'timedate22.' assigned to these vars will do.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Dec 2016 16:08:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Macro-if-then-else-into-Case/m-p/319968#M70385</guid>
      <dc:creator>kjohnsonm</dc:creator>
      <dc:date>2016-12-21T16:08:32Z</dc:date>
    </item>
  </channel>
</rss>

