<?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 Create Labels Dynamically? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-Labels-Dynamically/m-p/361986#M85464</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a series of varibles for the last 8 quarters named like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;qtr0_demand&lt;/P&gt;&lt;P&gt;qtr1_demand&lt;/P&gt;&lt;P&gt;qtr2_demand&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For presentation I would like the label to reflect the actual year qtr like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;label&amp;nbsp;&lt;/P&gt;&lt;P&gt;qtr0_demand = '2017-Q2'&lt;/P&gt;&lt;P&gt;qtr1_demand = '2017-Q1'&lt;/P&gt;&lt;P&gt;qtr2_demand = '2016-Q4'&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The year-qtr would need to update dynamically as we move into the next quarter &amp;nbsp;-I don't want to be touching this code to maintain the labels.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Tammy&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 26 May 2017 14:59:12 GMT</pubDate>
    <dc:creator>TMiles</dc:creator>
    <dc:date>2017-05-26T14:59:12Z</dc:date>
    <item>
      <title>Create Labels Dynamically?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Labels-Dynamically/m-p/361986#M85464</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a series of varibles for the last 8 quarters named like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;qtr0_demand&lt;/P&gt;&lt;P&gt;qtr1_demand&lt;/P&gt;&lt;P&gt;qtr2_demand&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For presentation I would like the label to reflect the actual year qtr like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;label&amp;nbsp;&lt;/P&gt;&lt;P&gt;qtr0_demand = '2017-Q2'&lt;/P&gt;&lt;P&gt;qtr1_demand = '2017-Q1'&lt;/P&gt;&lt;P&gt;qtr2_demand = '2016-Q4'&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The year-qtr would need to update dynamically as we move into the next quarter &amp;nbsp;-I don't want to be touching this code to maintain the labels.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Tammy&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 May 2017 14:59:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Labels-Dynamically/m-p/361986#M85464</guid>
      <dc:creator>TMiles</dc:creator>
      <dc:date>2017-05-26T14:59:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create Labels Dynamically?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Labels-Dynamically/m-p/361988#M85466</link>
      <description>&lt;P&gt;And how do I know that:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;qtr2_demand = '2016-Q4'&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;? &amp;nbsp;There isn't any information provided there that would say that variable is 2016 let alone the fact that the qtr2 is in direct contradiction to Q4 given??&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Also, post test data in the form of a datastep (a few rows) of each dataset used, this helps us see what your working with and don't have to go back and forth asking questions. &amp;nbsp;Likely you can (once we have some logic) generate the code to re-label these.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 26 May 2017 15:03:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Labels-Dynamically/m-p/361988#M85466</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-05-26T15:03:03Z</dc:date>
    </item>
    <item>
      <title>Re: Create Labels Dynamically?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Labels-Dynamically/m-p/362019#M85475</link>
      <description>&lt;P&gt;You know that qtr2_demand = '2016-Q4' because I have th Last 8 quarters based on today's date. &amp;nbsp;Below is my code -I tried to used Proc Tabulate to create the output table but there are too many crossing -so i am reverting to Proc Print and redefining the variables.&lt;/P&gt;&lt;PRE&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/9091iABD9F408BEC0DBD6/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="Proc Tabulate error" title="Proc Tabulate error" /&gt;data final(drop=fmgfirstadvdate ord_mail_dt);
  merge itemdata(in=a) items1(in=b) po2(in=c);
  by baseedp;
  format order_dt first_adv_dt mmddyy10.;
  if a=1;
  order_dt = datepart(ord_mail_dt);
  first_adv_dt = datepart(fmgfirstadvdate);
  qtr = put(intnx('qtr',order_dt,0,'b'),yyq8.);
  weeks = intck('week',order_dt,&amp;amp;lastweek);
  if weeks &amp;lt;= 52 then weeks52 = 0;
  else if weeks &amp;lt;= 104 then weeks52 = 1;
  else if weeks &amp;lt;= 156 then weeks52 = 2;
  else if weeks &amp;lt;= 208 then weeks52 = 3;
run;

data newfinal(drop=demandqty demandafterdisc cust_edp_id sales_ord_nr qtr weeks weeks52);
  set final;
  retain yr0_units yr0_dmd yr1_units yr1_dmd yr2_units yr2_dmd yr3_units yr3_dmd
         qtr0_units qtr0_dmd qtr1_units qtr1_dmd qtr2_units qtr2_dmd qtr3_units qtr3_dmd 
         qtr4_units qtr4_dmd qtr5_units qtr5_dmd qtr6_units qtr6_dmd qtr7_units qtr7_dmd qtr8_units qtr8_dmd;
  by baseedp;
  if first.baseedp then do;
    yr0_units = 0;
    yr0_dmd = 0;
    yr1_units = 0;
    yr1_dmd = 0;
    yr2_units = 0;
    yr2_dmd = 0;
    yr3_units = 0;
    yr3_dmd = 0;
    qtr0_units = 0;
    qtr0_dmd = 0;
    qtr1_units = 0;
    qtr1_dmd = 0;
    qtr2_units = 0;
    qtr2_dmd = 0;
    qtr3_units = 0;
    qtr3_dmd = 0;
    qtr4_units = 0;
    qtr4_dmd = 0;
    qtr5_units = 0;
    qtr5_dmd = 0;
    qtr6_units = 0;
    qtr6_dmd = 0;
    qtr7_units = 0;
    qtr7_dmd = 0;
    qtr8_units = 0;
    qtr8_dmd = 0;
  end;
  select(weeks52);
    when(0) do;
      yr0_units + demandqty;
      yr0_dmd + demandafterdisc;
    end;
    when(1) do;
      yr1_units + demandqty;
      yr1_dmd + demandafterdisc;
    end;
    when(2) do;
      yr2_units + demandqty;
      yr2_dmd + demandafterdisc;
    end;
    when(3) do;
      yr3_units + demandqty;
      yr3_dmd + demandafterdisc;
    end;
    otherwise;
  end;
  select(intck('qtr',order_dt,&amp;amp;lastweek));
    when(0) do;
      qtr0_units + demandqty;
      qtr0_dmd + demandafterdisc;
    end;
    when(1) do;
      qtr1_units + demandqty;
      qtr1_dmd + demandafterdisc;
    end;
    when(2) do;
      qtr2_units + demandqty;
      qtr2_dmd + demandafterdisc;
    end;
    when(3) do;
      qtr3_units + demandqty;
      qtr3_dmd + demandafterdisc;
    end;
    when(4) do;
      qtr4_units + demandqty;
      qtr4_dmd + demandafterdisc;
    end;
    when(5) do;
      qtr5_units + demandqty;
      qtr5_dmd + demandafterdisc;
    end;
    when(6) do;
      qtr6_units + demandqty;
      qtr6_dmd = demandafterdisc;
    end;
    when(7) do;
      qtr7_units + demandqty;
      qtr7_dmd + demandafterdisc;
    end;
    when(8) do;
      qtr8_units + demandqty;
      qtr8_dmd + demandafterdisc;
    end;
    otherwise;
  end;
  if last.baseedp then output;
run;

proc print data=newfinal noobs label;
  where lateststatus = 'C1' and onhand &amp;gt; 0 and sum(yr0_dmd,yr1_dmd,yr2_dmd,yr3_dmd) &amp;gt; 0;
  id first_adv_dt;
  var description lateststatus itemno last_po_dt first_po_dt onhand next_po_qty  
      yr0_units yr0_dmd yr1_units yr1_dmd yr2_units yr2_dmd yr3_units yr3_dmd
      qtr0_units qtr0_dmd qtr1_units qtr1_dmd qtr2_units qtr2_dmd qtr3_units qtr3_dmd 
      qtr4_units qtr4_dmd qtr5_units qtr5_dmd qtr6_units qtr6_dmd qtr7_units qtr7_dmd qtr8_units qtr8_dmd;  
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 May 2017 15:43:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Labels-Dynamically/m-p/362019#M85475</guid>
      <dc:creator>TMiles</dc:creator>
      <dc:date>2017-05-26T15:43:51Z</dc:date>
    </item>
    <item>
      <title>Re: Create Labels Dynamically?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Labels-Dynamically/m-p/362056#M85491</link>
      <description>&lt;P&gt;First, you are in desperate need of arrays and do loops.&amp;nbsp; Consider the reduction in typographical error risk in the code below as a replacement for your 'data newfinal;' step.&amp;nbsp; It's also easier to implement changes in logic as well:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data newfinal(drop=demandqty demandafterdisc cust_edp_id sales_ord_nr qtr weeks weeks52);
  set final;
  retain yr0_units yr0_dmd yr1_units yr1_dmd yr2_units yr2_dmd yr3_units yr3_dmd
         qtr0_units qtr0_dmd qtr1_units qtr1_dmd qtr2_units qtr2_dmd qtr3_units qtr3_dmd 
         qtr4_units qtr4_dmd qtr5_units qtr5_dmd qtr6_units qtr6_dmd qtr7_units qtr7_dmd qtr8_units qtr8_dmd;
  by baseedp;

  array qunits {0:8} qtr0_units qtr1_units qtr2_units qtr3_units qtr4_units qtr5_units qtr6_units qtr7_units qtr8_units ;
  array qdmd   {0:8} qtr0_dmd qtr1_dmd qtr2_dmd qtr3_dmd qtr4_dmd qtr5_dmd qtr6_dmd qtr7_dmd qtr8_dmd;

  array yunits {0:3} yr0_units yr1_units yr2_units yr3_units ;
  array ydmd   {0:3} yr0_dmd yr1_dmd yr2_dmd yr3_dmd ;

  if first.baseedp then do;
    do y=0 to 3;
      yunits{y}=0;
      ydmd{y}=0;
    end;
    do q=0 to 8;
      qunits{q}=0;
      qdmd{q}=0;
    end;
  end;

  if 0&amp;lt;=weeks52&amp;lt;=3 then do;
    yunits{weeks52}+demandqty;
    ydmd{weeks52}+demanmdafterdisc;
  end;

  qtr_offset=intck('qtr',order_dt,&amp;amp;lastweek);
  if 0&amp;lt;=qtr_offset&amp;lt;=8 then do;
    qunits{qtr_offset}+demandqty;
    qdmd{qtr_offset}+demandafterdisc;
  end;

  if last.baseedp;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now as to assigning labels to qtr0demand, etc.&amp;nbsp; You can write SAS label assignments to a temporary file, which can subsequently be "%include" in the program creating the variables.&amp;nbsp; For instance, precede your DATA step with this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename tmp temp ;
data _null_;&lt;BR /&gt;  file tmp;
  do q=0 to 8;
    date=intnx('qtr',today(),-1*q);
    put 'qtr' q +(-1) 'demand="' date yyq. '"';
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The symbolic filename TMP has the "temp" attribute.&amp;nbsp; I.e. it will be deleted when your sas session finishes.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Later, in the data step creating the variables of interest include these lines&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;label&lt;/P&gt;
&lt;P&gt;&amp;nbsp; %include TMP ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note there are two semi-colons - one to terminate tehe %include statement, and one to terminate the LABEL statement.&lt;/P&gt;</description>
      <pubDate>Fri, 26 May 2017 18:47:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Labels-Dynamically/m-p/362056#M85491</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-05-26T18:47:39Z</dc:date>
    </item>
  </channel>
</rss>

