2
votes

I'm trying to do some calculations based on different currencies where the output dataset consists of a column with company names and the following columns are the sum for each company in each specific currency (So one sum column per currency). I want to do the calculations through a loop where each currency is used, looping through the list of currencies. Since the sums for some of the currencies are very small or 0 I would like to manually specify the order in which the columns/currencies appear in the dataset, so the biggest values are in the first columns.

I've made the list of currencies in the desired order in a data step. When I read these values into a macro variable (using Proc Sql insert into...) and use a %put statement to see these values the order has been changed to alphabetical order. This causes the calculations to be done in the wrong order and thereby the columns in the output dataset are also in the wrong order.

Is there any way in which I can manage the order of the list and the calculation looping through the list of currencies, so they don't appear in alphabetical order but in the order that I want?

 %macro csa;
    data CSA_view;
    set csa;
    %do i=1 %to &number_of_curr;
    %let Curr = %scan(&currency_list, &i);
    keep counterparty mtm_&curr;
    if currency = "&curr" then MTM_&Curr = MTM;
%end;
run;
%mend;
%csa;
2

2 Answers

2
votes

When no order is specified in SQL the order you run the risk of it coming out in a way you don't want as you have in your case. How about adding an order variable(that you then ignore):

data curr ;
input ordvar currency $ ;
cards ;
1 USD
2 GBP
3 EUR
;run ;

You can then use this variable just as you were before but the order will be sorted by whatever you specify in the temporary variable ordvar:

proc sql ;
  select currency
  into :currency_list seperated by ' '
  from curr
  order by ordvar
;quit ;
1
votes

Another simple option is to use call symput to generate your list. The data step won't change the order.

data _null_;
  set your_list end=eof;
  retain big_list;
  length big_list $20000; *make sure this is big enough for your mvar list;
  big_list = catx(' ',big_list,your_Variable);
  if eof then 
    call symputx('mvarname',big_list);
run;