DATA Step, Macro, Functions and more

Create Labels Dynamically?

Accepted Solution Solved
Reply
Contributor
Posts: 43
Accepted Solution

Create Labels Dynamically?

Hi,

 

I have a series of varibles for the last 8 quarters named like this:

 

qtr0_demand

qtr1_demand

qtr2_demand

...

 

For presentation I would like the label to reflect the actual year qtr like this:

 

label 

qtr0_demand = '2017-Q2'

qtr1_demand = '2017-Q1'

qtr2_demand = '2016-Q4'

...

 

The year-qtr would need to update dynamically as we move into the next quarter  -I don't want to be touching this code to maintain the labels.

 

Thanks,

Tammy

 


Accepted Solutions
Solution
‎05-26-2017 04:33 PM
Trusted Advisor
Posts: 1,019

Re: Create Labels Dynamically?

First, you are in desperate need of arrays and do loops.  Consider the reduction in typographical error risk in the code below as a replacement for your 'data newfinal;' step.  It's also easier to implement changes in logic as well:

 

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<=weeks52<=3 then do;
    yunits{weeks52}+demandqty;
    ydmd{weeks52}+demanmdafterdisc;
  end;

  qtr_offset=intck('qtr',order_dt,&lastweek);
  if 0<=qtr_offset<=8 then do;
    qunits{qtr_offset}+demandqty;
    qdmd{qtr_offset}+demandafterdisc;
  end;

  if last.baseedp;
run;

 

Now as to assigning labels to qtr0demand, etc.  You can write SAS label assignments to a temporary file, which can subsequently be "%include" in the program creating the variables.  For instance, precede your DATA step with this:

 

filename tmp temp ;
data _null_;
file tmp; do q=0 to 8; date=intnx('qtr',today(),-1*q); put 'qtr' q +(-1) 'demand="' date yyq. '"'; end; run;

The symbolic filename TMP has the "temp" attribute.  I.e. it will be deleted when your sas session finishes. 

 

 

Later, in the data step creating the variables of interest include these lines

 

label

  %include TMP ;

  ;

 

Note there are two semi-colons - one to terminate tehe %include statement, and one to terminate the LABEL statement.

View solution in original post


All Replies
Super User
Super User
Posts: 7,948

Re: Create Labels Dynamically?

And how do I know that:

qtr2_demand = '2016-Q4'

?  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??

 

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.  Likely you can (once we have some logic) generate the code to re-label these. 

Contributor
Posts: 43

Re: Create Labels Dynamically?

You know that qtr2_demand = '2016-Q4' because I have th Last 8 quarters based on today's date.  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.

Proc Tabulate errordata 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,&lastweek);
  if weeks <= 52 then weeks52 = 0;
  else if weeks <= 104 then weeks52 = 1;
  else if weeks <= 156 then weeks52 = 2;
  else if weeks <= 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,&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 > 0 and sum(yr0_dmd,yr1_dmd,yr2_dmd,yr3_dmd) > 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;
Solution
‎05-26-2017 04:33 PM
Trusted Advisor
Posts: 1,019

Re: Create Labels Dynamically?

First, you are in desperate need of arrays and do loops.  Consider the reduction in typographical error risk in the code below as a replacement for your 'data newfinal;' step.  It's also easier to implement changes in logic as well:

 

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<=weeks52<=3 then do;
    yunits{weeks52}+demandqty;
    ydmd{weeks52}+demanmdafterdisc;
  end;

  qtr_offset=intck('qtr',order_dt,&lastweek);
  if 0<=qtr_offset<=8 then do;
    qunits{qtr_offset}+demandqty;
    qdmd{qtr_offset}+demandafterdisc;
  end;

  if last.baseedp;
run;

 

Now as to assigning labels to qtr0demand, etc.  You can write SAS label assignments to a temporary file, which can subsequently be "%include" in the program creating the variables.  For instance, precede your DATA step with this:

 

filename tmp temp ;
data _null_;
file tmp; do q=0 to 8; date=intnx('qtr',today(),-1*q); put 'qtr' q +(-1) 'demand="' date yyq. '"'; end; run;

The symbolic filename TMP has the "temp" attribute.  I.e. it will be deleted when your sas session finishes. 

 

 

Later, in the data step creating the variables of interest include these lines

 

label

  %include TMP ;

  ;

 

Note there are two semi-colons - one to terminate tehe %include statement, and one to terminate the LABEL statement.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 139 views
  • 1 like
  • 3 in conversation