<?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: confuse about %if statement in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/confuse-about-if-statement/m-p/927732#M41644</link>
    <description>&lt;PRE&gt;%if "&amp;amp;clase1"=A %then&lt;/PRE&gt;
&lt;P&gt;never true. The quotes are part of the comparison.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If all of the variables likely to be used in this macro exist in the data set, i.e. the data set always has the same structure just using different versions of the data, then just have all the format statements as part of the code. The worst that would happen is if the variable in the FORMAT statement doesn't exist is a warning in the log. If the variables are there but not used you don't have to worry about any of these conditional assignments.&lt;/P&gt;
&lt;P&gt;If you conditions involve style elements or table structure that is a different story.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, if you are using the macro to make multiple tables you may consider multiple table statements in Proc Tabulate and not have to make multiple calls. Many people don't realize that Proc Tabulate (and Freq) will allow multiple different table statements such as:&lt;/P&gt;
&lt;PRE&gt;proc tabulate data=sashelp.class;
   class sex age;
   var height weight;

   table sex,age;
   table sex*age,
         height*(min mean max);
   table sex,
         age*weight*(mean stddev)
   ;
run;&lt;/PRE&gt;
&lt;P&gt;Tabulate will require that a variable be used in the same role for all table requests though. So if you want to use a variable as a CLASS variable in one table and VAR variable in a different table then multiple calls to tabulate will be required.&lt;/P&gt;</description>
    <pubDate>Thu, 09 May 2024 15:16:32 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2024-05-09T15:16:32Z</dc:date>
    <item>
      <title>confuse about %if statement</title>
      <link>https://communities.sas.com/t5/New-SAS-User/confuse-about-if-statement/m-p/927722#M41640</link>
      <description>&lt;P&gt;hello everyone,&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%macro p2(datos,clase1,clase2);
    proc tabulate data=&amp;amp;datos format=commax10.;&lt;BR /&gt;       class ANYO;
       class &amp;amp;clase1 &amp;amp;clase2/ missing preloadfmt order=data mlf;&lt;BR /&gt;       tables &amp;amp;clase1*&amp;amp;clase2 all="Total";&lt;BR /&gt;%if "&amp;amp;clase1"=A %then .............;
%if "&amp;amp;clase1"=B %then .............;
%if "&amp;amp;clase1"=C %then .............;
%if "&amp;amp;clase1"=D %then .............;
%if "&amp;amp;clase1"=E %then .............;&lt;BR /&gt;..............;&lt;BR /&gt;%if "&amp;amp;clase1"=X %then .............;
%if "&amp;amp;clase1"=Y %then .............;
%if "&amp;amp;clase1"=Z %then .............;
%if "&amp;amp;clase1"="SEXO" %then
	format &amp;amp;clase1 SEXO.;
    run;
%mend;

%p1(people,SEXO,EDAD);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I get this message: "&lt;SPAN&gt;WARNING: Variable RUN not found in data set WORK.people&lt;/SPAN&gt;" What does it mean?? Why I get it and how I can &lt;STRONG&gt;solve it without %do; ... %end;&lt;/STRONG&gt;??&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 May 2024 14:49:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/confuse-about-if-statement/m-p/927722#M41640</guid>
      <dc:creator>ppinedo</dc:creator>
      <dc:date>2024-05-09T14:49:16Z</dc:date>
    </item>
    <item>
      <title>Re: confuse about %if statement</title>
      <link>https://communities.sas.com/t5/New-SAS-User/confuse-about-if-statement/m-p/927727#M41641</link>
      <description>&lt;P&gt;You need a semicolon after format statement, the semicolon you have there ends %if-%then block&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro p2(datos,clase1,clase2);
    proc tabulate data=&amp;amp;datos format=commax10.;
 class &amp;amp;clase1 &amp;amp;clase2/ missing preloadfmt order=data mlf;
tables &amp;amp;clase1*&amp;amp;clase2 all="Total";
%if "&amp;amp;clase1"=A %then .............;
%if "&amp;amp;clase1"=B %then .............;
%if "&amp;amp;clase1"=C %then .............;
%if "&amp;amp;clase1"=D %then .............;
%if "&amp;amp;clase1"=E %then .............;
..............;
%if "&amp;amp;clase1"=X %then .............;
%if "&amp;amp;clase1"=Y %then .............;
%if "&amp;amp;clase1"=Z %then .............;
%if "&amp;amp;clase1"="SEXO" %then
	format &amp;amp;clase1 SEXO.; /* &amp;lt;- THIS semicolon ends the %if-%then statement NOT format statement */
    run;
%mend;

%p1(people,SEXO,EDAD);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Good practice: use %do; -%end; groups&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 09 May 2024 14:51:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/confuse-about-if-statement/m-p/927727#M41641</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-05-09T14:51:56Z</dc:date>
    </item>
    <item>
      <title>Re: confuse about %if statement</title>
      <link>https://communities.sas.com/t5/New-SAS-User/confuse-about-if-statement/m-p/927731#M41643</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;, if there's only one sentence why I need to close %if&lt;span class="lia-unicode-emoji" title=":thinking_face:"&gt;🤔&lt;/span&gt;? if I use more than one sentence, can I use double semicolon ??&lt;/P&gt;&lt;P&gt;&lt;CODE class=""&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;..................;
.................;
%if "&amp;amp;clase1"="SEXO_1F" %then
	format &amp;amp;clase1 SEXO.;;
    run;
%mend;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 May 2024 15:07:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/confuse-about-if-statement/m-p/927731#M41643</guid>
      <dc:creator>ppinedo</dc:creator>
      <dc:date>2024-05-09T15:07:15Z</dc:date>
    </item>
    <item>
      <title>Re: confuse about %if statement</title>
      <link>https://communities.sas.com/t5/New-SAS-User/confuse-about-if-statement/m-p/927732#M41644</link>
      <description>&lt;PRE&gt;%if "&amp;amp;clase1"=A %then&lt;/PRE&gt;
&lt;P&gt;never true. The quotes are part of the comparison.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If all of the variables likely to be used in this macro exist in the data set, i.e. the data set always has the same structure just using different versions of the data, then just have all the format statements as part of the code. The worst that would happen is if the variable in the FORMAT statement doesn't exist is a warning in the log. If the variables are there but not used you don't have to worry about any of these conditional assignments.&lt;/P&gt;
&lt;P&gt;If you conditions involve style elements or table structure that is a different story.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, if you are using the macro to make multiple tables you may consider multiple table statements in Proc Tabulate and not have to make multiple calls. Many people don't realize that Proc Tabulate (and Freq) will allow multiple different table statements such as:&lt;/P&gt;
&lt;PRE&gt;proc tabulate data=sashelp.class;
   class sex age;
   var height weight;

   table sex,age;
   table sex*age,
         height*(min mean max);
   table sex,
         age*weight*(mean stddev)
   ;
run;&lt;/PRE&gt;
&lt;P&gt;Tabulate will require that a variable be used in the same role for all table requests though. So if you want to use a variable as a CLASS variable in one table and VAR variable in a different table then multiple calls to tabulate will be required.&lt;/P&gt;</description>
      <pubDate>Thu, 09 May 2024 15:16:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/confuse-about-if-statement/m-p/927732#M41644</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-05-09T15:16:32Z</dc:date>
    </item>
    <item>
      <title>Re: confuse about %if statement</title>
      <link>https://communities.sas.com/t5/New-SAS-User/confuse-about-if-statement/m-p/927734#M41645</link>
      <description>&lt;P&gt;ok. &lt;STRONG&gt;you're right&lt;/STRONG&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;, I wanted to say:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%if &amp;amp;clase1=A %then ..........&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 May 2024 15:22:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/confuse-about-if-statement/m-p/927734#M41645</guid>
      <dc:creator>ppinedo</dc:creator>
      <dc:date>2024-05-09T15:22:31Z</dc:date>
    </item>
    <item>
      <title>Re: confuse about %if statement</title>
      <link>https://communities.sas.com/t5/New-SAS-User/confuse-about-if-statement/m-p/927735#M41646</link>
      <description>&lt;P&gt;When you have a single statement that spans multiple lines it will make the program much easier for humans to read if you place the semicolon that ends the statement on its own line.&amp;nbsp; So your %IF statement should look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if "&amp;amp;clase1"="SEXO" %then
  format &amp;amp;clase1 SEXO.
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now it is more obvious that the FORMAT statement does not have an ending semicolon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But it is better practice when a macro statement like %IF is controlling the generation of complete statements (not just fragments) to use explicit %DO/%END block.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if "&amp;amp;clase1"="SEXO" %then %do;
  format &amp;amp;clase1 SEXO. ;
%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 May 2024 15:35:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/confuse-about-if-statement/m-p/927735#M41646</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-05-09T15:35:57Z</dc:date>
    </item>
    <item>
      <title>Re: confuse about %if statement</title>
      <link>https://communities.sas.com/t5/New-SAS-User/confuse-about-if-statement/m-p/927736#M41647</link>
      <description>&lt;P&gt;You need a semicolon to end the macro language statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;%if "&amp;amp;clase1"="SEXO_1F" %then&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;format &amp;amp;clase1 SEXO.&lt;/FONT&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;run;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;%mend;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Mind the order of colours! Red semicolon and &lt;EM&gt;then the&amp;nbsp;&lt;/EM&gt;blue one,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the same way you would "close" the put statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;FONT color="#FF0000"&gt;%put&lt;/FONT&gt; some text to print &lt;FONT color="#FF0000"&gt;;&amp;nbsp;/* this semicolon ends the %put statement */&lt;/FONT&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have a 4GL code which contains semicolons you basically have to use %do;-%end; block&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This will fail:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro p1(datos,clase1,clase2);

data _nulL_;

%if "&amp;amp;clase1"="SEXO" %then
	format &amp;amp;clase1 dollar12.2; &amp;amp;clase1=17; y=42; ;

run;
%mend;

%p1(people,SEXO,EDAD);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;because the semicolon after "&lt;CODE class=" language-sas"&gt;dollar12.2&lt;/CODE&gt;" is interpreted as %if-%then statement end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Log looks like this:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;1    %macro p1(datos,clase1,clase2);
2
3    data _nulL_;
4
5    %if "&amp;amp;clase1"="SEXO" %then
6      format &amp;amp;clase1 dollar12.2; &amp;amp;clase1=17; y=42; ;
7
8    run;
9    %mend;
10
11   %p1(people,SEXO,EDAD);
NOTE: Line generated by the invoked macro "P1".
6     &amp;amp;clase1=17; y=42; ;  run;
             -
             85
             76

ERROR 85-322: Expecting a format name.

ERROR 76-322: Syntax error, statement will be ignored.

NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds
&lt;/LI-CODE&gt;
&lt;P&gt;But if you add %do;-%end; block everything works fine:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro p1(datos,clase1,clase2);

data _nulL_;

%if "&amp;amp;clase1"="SEXO" %then 
  %do;
	format &amp;amp;clase1 dollar12.2; &amp;amp;clase1=17; y=42; 
  %end;

run;
%mend;

%p1(people,SEXO,EDAD);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;1    %macro p1(datos,clase1,clase2);
2
3    data _nulL_;
4
5    %if "&amp;amp;clase1"="SEXO" %then
6      %do;
7      format &amp;amp;clase1 dollar12.2; &amp;amp;clase1=17; y=42;
8      %end;
9
10   run;
11   %mend;
12
13   %p1(people,SEXO,EDAD);

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Remember all the "hardness" of macro programming is that your code (the macro language statements) and your data (the 4GL code) are mixed together in one place (a text file with code), so you have to have precise rules to tell SAS which is which.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 09 May 2024 15:43:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/confuse-about-if-statement/m-p/927736#M41647</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-05-09T15:43:11Z</dc:date>
    </item>
  </channel>
</rss>

