<?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: Conditional Labelling in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Labelling/m-p/393286#M277815</link>
    <description>&lt;P&gt;A label cannot be conditional. The label for a variable is a constant.&lt;/P&gt;
&lt;P&gt;But the whole idea of a macro is to conditionally generate code. So yes your macro could conditionally generate the label based on the value of the macro variable that holds the name of the actual variable.&lt;/P&gt;
&lt;P&gt;You could just use %IF/%THEN logic.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;t1.SUM_of__Calculation_Sum
%if (&amp;amp;i = CYVR ) %then LABEL="Arrivals at Vancouver" ;
%else %if (&amp;amp;i=CYYC) %then LABEL="Arrivals at XXXX" ;
AS &amp;amp;i._ARR &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might want to either pass in the names associated with the airport codes, or define a format, or have a table you can query to find the name. &amp;nbsp;So if you had a format named $AIRPORT then your code would look like this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;t1.SUM_of__Calculation_Sum LABEL="Arrivals at %sysfunc(putc(&amp;amp;i,$airport))" AS &amp;amp;i._ARR&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you needed to query a table then add another local macro varaible and populate it before the step where you need it. Make sure to intialize the new macro variable in case the query does not match any record in your metadata table.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint ;
  %let airport=&amp;amp;i;
  select airport into :airport trimmed 
    from code_to_airport
    where code="&amp;amp;i"
  ;
  create table egtask.arr_&amp;amp;i as 
    select t1.date_utc
         , year(t1.date_utc) as year
         , month(t1.date_utc) as month
         , t1.sum_of__calculation_sum label="Arrivals at &amp;amp;airport" as &amp;amp;i._arr
    from means_tmp t1
    where t1.date_utc &amp;lt;= intnx('month',today(),-1,'e')
  ;
quit;&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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 05 Sep 2017 16:14:58 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-09-05T16:14:58Z</dc:date>
    <item>
      <title>Conditional Labelling</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Labelling/m-p/393275#M277814</link>
      <description>&lt;P&gt;I have a macro that grabs data as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro airport_data (airport_list=);
%local n i;
%do n=1 %to %sysfunc(countw(&amp;amp;airport_list));
	%let i=%scan(&amp;amp;airport_list,&amp;amp;n);

some code

PROC SQL;
 		CREATE TABLE EGTASK.ARR_&amp;amp;i AS 
   		SELECT t1.DATE_UTC, 
     	       (year(t1.DATE_UTC)) AS Year, 
        	   (month(t1.DATE_UTC)) AS Month, 
        		t1.SUM_of__Calculation_Sum LABEL="Arrivals at Vancouver" AS &amp;amp;i._ARR
      	FROM MEANS_TMP t1
      	WHERE t1.DATE_UTC &amp;lt;= intnx('days',today(),-day(today()));
	QUIT;

	proc datasets nolist nodetails;
		delete means_tmp query_tmp SORTTEMPTABLESORTED_; run;
%end;
%mend;
%airport_data (airport_list=CYVR CYYC);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I would like to be able to label the data conditional on the airport code being used.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, if the airport_list=CYVR, I would like:&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;t1.SUM_of__Calculation_Sum LABEL=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"Arrivals at Vancouver"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; AS &amp;amp;i._ARR&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Now if CYYC comes up I would like:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New"&gt;t1.SUM_of__Calculation_Sum LABEL=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"Arrivals at Calgary"&lt;/FONT&gt; AS &amp;amp;i._ARR&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Can SAS use a conditional on the label?&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Sep 2017 15:50:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Labelling/m-p/393275#M277814</guid>
      <dc:creator>BCNAV</dc:creator>
      <dc:date>2017-09-05T15:50:51Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Labelling</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Labelling/m-p/393286#M277815</link>
      <description>&lt;P&gt;A label cannot be conditional. The label for a variable is a constant.&lt;/P&gt;
&lt;P&gt;But the whole idea of a macro is to conditionally generate code. So yes your macro could conditionally generate the label based on the value of the macro variable that holds the name of the actual variable.&lt;/P&gt;
&lt;P&gt;You could just use %IF/%THEN logic.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;t1.SUM_of__Calculation_Sum
%if (&amp;amp;i = CYVR ) %then LABEL="Arrivals at Vancouver" ;
%else %if (&amp;amp;i=CYYC) %then LABEL="Arrivals at XXXX" ;
AS &amp;amp;i._ARR &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might want to either pass in the names associated with the airport codes, or define a format, or have a table you can query to find the name. &amp;nbsp;So if you had a format named $AIRPORT then your code would look like this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;t1.SUM_of__Calculation_Sum LABEL="Arrivals at %sysfunc(putc(&amp;amp;i,$airport))" AS &amp;amp;i._ARR&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you needed to query a table then add another local macro varaible and populate it before the step where you need it. Make sure to intialize the new macro variable in case the query does not match any record in your metadata table.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint ;
  %let airport=&amp;amp;i;
  select airport into :airport trimmed 
    from code_to_airport
    where code="&amp;amp;i"
  ;
  create table egtask.arr_&amp;amp;i as 
    select t1.date_utc
         , year(t1.date_utc) as year
         , month(t1.date_utc) as month
         , t1.sum_of__calculation_sum label="Arrivals at &amp;amp;airport" as &amp;amp;i._arr
    from means_tmp t1
    where t1.date_utc &amp;lt;= intnx('month',today(),-1,'e')
  ;
quit;&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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Sep 2017 16:14:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Labelling/m-p/393286#M277815</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-09-05T16:14:58Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Labelling</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Labelling/m-p/393290#M277816</link>
      <description>&lt;P&gt;Best way would be to build a format based on the airport code, I am sure you have a data set that has airport codes and cities.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You then can use the format together with %SYSFUNC and the PUTC function within a macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a simpel example&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
  value $apcode
    "CYVR" = "Vancouver"
    "CYYC" = "Calgary"
  ;
run;

%let apcode = CYVR;
%put NOTE: &amp;amp;apcode is %sysfunc(putc(&amp;amp;apcode, $apcode.));

%let apcode = CYYC;
%put NOTE: &amp;amp;apcode is %sysfunc(putc(&amp;amp;apcode, $apcode.));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Sep 2017 16:18:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Labelling/m-p/393290#M277816</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2017-09-05T16:18:20Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Labelling</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Labelling/m-p/393293#M277817</link>
      <description>&lt;P&gt;By using "some code" you hide what the name of the variable is that holds the values of your Airport.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I think you are doing may very well be handled by a custom format to use associate values of "Vancouver" with "CYVR" and then by group processing instead of macro loops or possibly a different report procedure with a where statement and by group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An example with proc tabulate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc format library=work;
value $Airport
'CYVR' ='Vancouver'
'CYYC' ='Calgary'
;
run;

data junk;
   do Airport = 'CYVR','CYYC';
      do date_utc='21Jan2017'd to '10FEB2017'd;
         arrivals= round(rand('uniform')*25);
         month=Month(date_utc);
         year =year(date_utc);
         output;
      end;
   end;
run;

proc tabulate data=junk;
   class airport year month;
   format airport  $airport.;
   var arrivals;
   table airport='Arrivals at: ',
         year*month,
         arrivals='Arrivals'*sum
   ;
run;&lt;/PRE&gt;
&lt;P&gt;Note that it may very well be that depending on your actual data a single proc tabulate or report could do what you are requesting as those procedures do pretty good jobs of addition based on class (proc tabulate) or group and order (proc report) variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Sep 2017 16:28:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Labelling/m-p/393293#M277817</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-09-05T16:28:24Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Labelling</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Labelling/m-p/393294#M277818</link>
      <description>&lt;P&gt;Depending on how the rest of your process works you probably can simplify your whole process by using BY processing.&lt;/P&gt;
&lt;P&gt;Then you could just use PROC TRANSPOSE to convert ariport metadata into the variable and/or label name. &amp;nbsp;Then you don't need to generate code as PROC TRANSPOSE can generate the name and/or label from data in the table.&lt;/P&gt;
&lt;P&gt;So perhpas something along these lines.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=have ;
  by .... ;
  where code in ("CYVR" "CYYC");
  var xxx;
  output out=tall sum=SUM_of__Calculation_sum ;
run;

data for_transpose ;
  set tall;
  length _name_ $32 _label_ $256 ;
  _name_ = cats(code,'_ARR');
  _label_ = catx(' ','Arrivals at',put(code,$airport.));
run;

proc transpose data=for_transpose out=want ;
  by .... ;
  var SUM_of__Calculation_sum
  id =_name_;
  idlabel = _label_;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Sep 2017 16:28:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Labelling/m-p/393294#M277818</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-09-05T16:28:43Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Labelling</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Labelling/m-p/393877#M277819</link>
      <description>&lt;P&gt;Thank you all for your help. It has been fun to play with the solutions. Both the "if" form of labelling and proc format are nice, but I chose to utilize proc format for all Canadian air traffic control towers:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
	/* These are all 41 towers in Canada as at Sept. 2017 */
	value $TOWERS_ICAO
		"CYXX" = "Abbotsford"
		"CZBB" = "Boundary Bay"
		"CYYC" = "Calgary"
		"CYBW" = "Calgary/Springbank"
		"CYRC" = "Chicoutmi/Saint Honoré"
		"CYEG" = "Edmonton"
		"CZVL" = "Edmonton/Villeneuve"
		"CYFC" = "Frederiction"
		"CYMM" = "Fort McMurray"
		"CYQX" = "Gander"
		"CYHZ" = "Halifax"
		"CYHM" = "Hamilton"
		"CYLW" = "Kelowna"
		"CYKF" = "Kitchener-Waterloo"
		"CYLY" = "Langley"
		"CYXU" = "London"
		"CYQM" = "Moncton"
		"CYUL" = "Montréal/Dorval"
		"CYHU" = "Montréal/Saint-Hubert"
		"CYOO" = "Oshawa"
		"CYOW" = "Ottawa"
		"CYPK" = "Pitt Meadows"
		"CYXS" = "Prince George"
		"CYQB" = "Québec"
		"CYQR" = "Regina"
		"CYJN" = "Saint-Jean"
		"CYXE" = "Saskatoon"
		"CYAM" = "Sault Ste. Marie"
		"CYYT" = "St. John's"
		"CYQT" = "Thunder Bay"
		"CYTZ" = "Toronto Island"
		"CYYZ" = "Toronto Pearson"
		"CYKZ" = "Toronto Buttonville"
		"CCXH" = "Vancouver Harbour"
		"CYVR" = "Vancouver and Waterdrome"
		"CYYJ" = "Victoria"
		"CYXY" = "Whitehorse"
		"CYQG" = "Windsor"
		"CYWG" = "Winnipeg"
		"CYAV" = "Winnipeg/St. Andrews"
		"CYZF" = "Yellowknife"
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;thanks again!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Sep 2017 14:04:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Labelling/m-p/393877#M277819</guid>
      <dc:creator>BCNAV</dc:creator>
      <dc:date>2017-09-07T14:04:42Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Labelling</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Labelling/m-p/394005#M277820</link>
      <description>&lt;P&gt;Custom formats are very helpful.&lt;/P&gt;
&lt;P&gt;You may also find that mutilabel format is useful. In your airport&amp;nbsp;list it might be that at some time you want to summarize based on a service region and have the individual airports reported as well. A multilabel format allows you to assign a region value as well as the individual.&lt;/P&gt;
&lt;P&gt;Only a few procedures, Procs Report, Tabulate, Means/Summary can use the multiple nature but once you have them set up they can be very helpful. I use this approach to map service locations to regions and to have multiple age groups such as 15-21 and 15-17 18-21.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Sep 2017 19:24:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Labelling/m-p/394005#M277820</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-09-07T19:24:18Z</dc:date>
    </item>
  </channel>
</rss>

