<?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: IF-THEN in Enterprise Guide Code in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913619#M44224</link>
    <description>&lt;P&gt;Creates a local macro variable named N with the number of observations in the dataset named WORK.CAPS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first line starts a data step.&amp;nbsp; The _NULL_ means that no output dataset will be written.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second line creates the macro variable.&amp;nbsp; The first argument is the name of the macro variable to create, the second is the value to put into the macro variable and the last says to make the variable as local, even if there already exists another macro variable in a different scope with that same name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The third line ends the execution of the data step so that it only iterates once, no matter how many observations the input dataset has.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The fourth line says to read from WORK.CAPS and set the variable named NOBS to the number of observations that WORK.CAPS has.&amp;nbsp; The value of NOBS is set before the data step starts executing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The last line ends the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 30 Jan 2024 19:45:07 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-01-30T19:45:07Z</dc:date>
    <item>
      <title>IF-THEN in Enterprise Guide Code</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913476#M44215</link>
      <description>&lt;P&gt;I need to use a query in (3) when the number of records in table 1 is zero and another query when the result is greater than zero.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="sas.jpg" style="width: 534px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/93055i484699B57DF71ACB/image-size/large?v=v2&amp;amp;px=999" role="button" title="sas.jpg" alt="sas.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I tried the code(inside 3) below but IF-THEN didn't work.&lt;/P&gt;&lt;P&gt;can anybody help me?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql noprint;
select count(*) into :N from WORK.CAPS;
*/%put &amp;amp;N;
quit;


PROC SQL;
if &amp;amp;N = 0 then

   CREATE TABLE WORK.QUERY_FOR_CAPS AS 
   SELECT t1.CPF, 
          t1.DSC_NOME_PESSOA_FISICA, 
          t1.PRIMEIRO_NOME
      FROM WORK.CAP5 t1

else
   CREATE TABLE WORK.QUERY_FOR_CAPS AS 
   SELECT t2.CPF, 
          t2.DSC_NOME_PESSOA_FISICA, 
          t2.PRIMEIRO_NOME
      FROM WORK.CAPS t1
           INNER JOIN WORK.CAP5 t2 ON (t1.PRIMEIRO_NOME = t2.PRIMEIRO_NOME);

QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jan 2024 21:31:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913476#M44215</guid>
      <dc:creator>fabiopjr</dc:creator>
      <dc:date>2024-01-29T21:31:22Z</dc:date>
    </item>
    <item>
      <title>Re: IF-THEN in Enterprise Guide Code</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913477#M44216</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead of using:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if &amp;amp;N = 0 then&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try using:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;N = 0 %then
%do;
  /* place code here to run if there are 0 obs */
%end;
%else
%do;
  /* place code here to run if there are one or more obs */
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &lt;FONT face="courier new,courier"&gt;%do;&lt;/FONT&gt; &lt;FONT face="courier new,courier"&gt;%end;&lt;/FONT&gt;&amp;nbsp;and %else&amp;nbsp;&lt;SPAN&gt;are required.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: Add %else and formatting.&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jan 2024 21:49:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913477#M44216</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2024-01-29T21:49:59Z</dc:date>
    </item>
    <item>
      <title>Re: IF-THEN in Enterprise Guide Code</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913479#M44217</link>
      <description>&lt;P&gt;Hi, Amir.&lt;/P&gt;&lt;P&gt;Thanks for your response.&lt;/P&gt;&lt;P&gt;I tried this:.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql noprint;
select count(*) into :N from WORK.CAPS;
*/%put &amp;amp;N;
quit;


PROC SQL;
%if &amp;amp;N = 0 %then
	%do;

	   CREATE TABLE WORK.QUERY_FOR_CAPS AS 
	   SELECT t1.CPF, 
	          t1.DSC_NOME_PESSOA_FISICA, 
	          t1.PRIMEIRO_NOME
	      FROM WORK.CAP5 t1

	%end;

%else
	%do;
		CREATE TABLE WORK.QUERY_FOR_CAPS AS 
		SELECT t2.CPF, 
		      t2.DSC_NOME_PESSOA_FISICA, 
		      t2.PRIMEIRO_NOME
		  FROM WORK.CAPS t1
		       INNER JOIN WORK.CAP5 t2 ON (t1.PRIMEIRO_NOME = t2.PRIMEIRO_NOME);
	%end;

QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But I received the following error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Captura de tela 2024-01-29 191121.png" style="width: 609px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/93056i01BAD70C9E0CBF1A/image-size/large?v=v2&amp;amp;px=999" role="button" title="Captura de tela 2024-01-29 191121.png" alt="Captura de tela 2024-01-29 191121.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jan 2024 22:12:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913479#M44217</guid>
      <dc:creator>fabiopjr</dc:creator>
      <dc:date>2024-01-29T22:12:07Z</dc:date>
    </item>
    <item>
      <title>Re: IF-THEN in Enterprise Guide Code</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913480#M44218</link>
      <description>&lt;P&gt;Since you are using an older version of SAS you will need to contain the macro statements inside a macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro SQL_Code;

PROC SQL;
%if &amp;amp;N = 0 %then
	%do;

	   CREATE TABLE WORK.QUERY_FOR_CAPS AS 
	   SELECT t1.CPF, 
	          t1.DSC_NOME_PESSOA_FISICA, 
	          t1.PRIMEIRO_NOME
	      FROM WORK.CAP5 t1

	%end;

%else
	%do;
		CREATE TABLE WORK.QUERY_FOR_CAPS AS 
		SELECT t2.CPF, 
		      t2.DSC_NOME_PESSOA_FISICA, 
		      t2.PRIMEIRO_NOME
		  FROM WORK.CAPS t1
		       INNER JOIN WORK.CAP5 t2 ON (t1.PRIMEIRO_NOME = t2.PRIMEIRO_NOME);
	%end;

QUIT;

%mend SQL_Code;
%SQL_Code;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Jan 2024 22:56:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913480#M44218</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2024-01-29T22:56:13Z</dc:date>
    </item>
    <item>
      <title>Re: IF-THEN in Enterprise Guide Code</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913482#M44219</link>
      <description>&lt;P&gt;You must be on a older maintenance release that's prior to the introduction of macro %IF statements in open code.&lt;/P&gt;
&lt;P&gt;Below code should work for you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro doit();
  data _null_;
    call symputx('N',nobs,'l');
    stop;
    set work.caps nobs=nobs;
  run;

  PROC SQL;
    %if &amp;amp;N = 0 %then
      %do;
        CREATE TABLE WORK.QUERY_FOR_CAPS AS 
          SELECT t1.CPF, 
            t1.DSC_NOME_PESSOA_FISICA, 
            t1.PRIMEIRO_NOME
          FROM WORK.CAP5 t1;
      %end;
    %else
      %do;
        CREATE TABLE WORK.QUERY_FOR_CAPS AS 
          SELECT t2.CPF, 
            t2.DSC_NOME_PESSOA_FISICA, 
            t2.PRIMEIRO_NOME
          FROM WORK.CAPS t1
            INNER JOIN WORK.CAP5 t2 ON (t1.PRIMEIRO_NOME = t2.PRIMEIRO_NOME);
      %end;
  QUIT;
%mend;
%doit();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I've also replaced the select count(*) into logic with a SAS data _null_ step because with SAS tables the number of rows in the table is stored in the descriptor ("header") portion of the table.&lt;/P&gt;
&lt;P&gt;A select count(*) needs to read all the data while the data _null_ step I've posted always only needs to read the descriptor portion of the table which is rather beneficial should you potentially have a lot of rows stored in the table.&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jan 2024 23:08:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913482#M44219</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-01-29T23:08:32Z</dc:date>
    </item>
    <item>
      <title>Re: IF-THEN in Enterprise Guide Code</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913486#M44220</link>
      <description>&lt;P&gt;This is why it helps to state the version of SAS you are using.&amp;nbsp; (The version of Enterprise Guide does not matter here.&amp;nbsp; Instead you need to tell us the version of SAS that you have connected to from Enterprise Guide.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also why are you using such an old version of SAS?&amp;nbsp; You still have to pay the license fees every year, you might as well use the latest version so you can use all of the new features.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2024 00:18:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913486#M44220</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-01-30T00:18:52Z</dc:date>
    </item>
    <item>
      <title>Re: IF-THEN in Enterprise Guide Code</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913487#M44221</link>
      <description>&lt;P&gt;Why?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When there are no observations in CAPS those two queries will generate the exact same results, an empty dataset with three variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So why not just always run the second one?&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2024 00:22:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913487#M44221</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-01-30T00:22:51Z</dc:date>
    </item>
    <item>
      <title>Re: IF-THEN in Enterprise Guide Code</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913617#M44223</link>
      <description>&lt;P&gt;Patrick or someone else,&lt;/P&gt;&lt;P&gt;Could you explain this piece of code to me?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;  data _null_;
    call symputx('N',nobs,'l');
    stop;
    set work.caps nobs=nobs;
  run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Jan 2024 16:49:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913617#M44223</guid>
      <dc:creator>fabiopjr</dc:creator>
      <dc:date>2024-01-30T16:49:40Z</dc:date>
    </item>
    <item>
      <title>Re: IF-THEN in Enterprise Guide Code</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913619#M44224</link>
      <description>&lt;P&gt;Creates a local macro variable named N with the number of observations in the dataset named WORK.CAPS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first line starts a data step.&amp;nbsp; The _NULL_ means that no output dataset will be written.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second line creates the macro variable.&amp;nbsp; The first argument is the name of the macro variable to create, the second is the value to put into the macro variable and the last says to make the variable as local, even if there already exists another macro variable in a different scope with that same name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The third line ends the execution of the data step so that it only iterates once, no matter how many observations the input dataset has.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The fourth line says to read from WORK.CAPS and set the variable named NOBS to the number of observations that WORK.CAPS has.&amp;nbsp; The value of NOBS is set before the data step starts executing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The last line ends the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2024 19:45:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913619#M44224</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-01-30T19:45:07Z</dc:date>
    </item>
    <item>
      <title>Re: IF-THEN in Enterprise Guide Code</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913620#M44225</link>
      <description>Thank you very much.</description>
      <pubDate>Tue, 30 Jan 2024 16:59:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/IF-THEN-in-Enterprise-Guide-Code/m-p/913620#M44225</guid>
      <dc:creator>fabiopjr</dc:creator>
      <dc:date>2024-01-30T16:59:33Z</dc:date>
    </item>
  </channel>
</rss>

