Statistical programming, matrix languages, and more

lists

Reply
Occasional Contributor
Posts: 19

lists

Is there a way to create a list of elements in SAS?

 

I am trying to replicate the R code:

 

new_hand = function(shoe, cards = shoe(2), bet = 1)

{

list(bet = bet, shoe = shoe, cards = cards)

}

 

Shoe is already a previous created function (or in SAS, macro). I am trying to create a new macro called new_hand that is similar to this R function.

Grand Advisor
Posts: 17,333

Re: lists

It's probably better to use SAS functionality. 

 

This looks like a recode/lookup function? If so, I'd use a format. 

SAS Super FREQ
Posts: 3,406

Re: lists

Although R and SAS/IML share a lot of functionality and serve the same computational audience, SAS/IML does not have the same variety of data structures as R does. In particular, SAS/IML does not support lists natively, although there are various work-arounds.

The trick that I use most often is to define an array of matrices.  Another trick is to maintain three separate arrays, one for bets, one for shoes, and one for cards.  The i_th row of each matrix refers to the same hand, so are related.

 

For example, to encode N poker hands and bets, you could use

N = 3;

bets = j(N, 1, .);  /* i_th row is bet for i_th hand (or i_th player) */

cards = j(N, 5, .);

do i = 1 to N;

   /* simulate bets and deals */

  cards[i,] = deal_the_hand(...args...);

  bets[i] = determine_the_bet(...args...); 

end;

 

For dealing, you might want to look at the SAMPLE function.

 

One last thought: SAS/IML has functions (sometimes called modules). You can define functions and supply default parameter values for optional arguments. 

 

It sounds like you might be a novice programmer in SAS/IML. If so, you might want to read "Ten tips for learning the SAS/IML language." A key to being a successful programmer in any language is to learn how to think in that language. Sometimes new programmers try to duplicate paradigms that work well in another language, but do not extend to the new language.  I remember being proud of a LISP program that I wrote in the 1980s.  My colleague commented that "this is the best FORTRAN program translated into LISP" that he had ever seen.  He then showed me how to think in LISP.

Grand Advisor
Posts: 9,576

Re: lists

Then show us your data,output and the function you want to achieve .

In IML ,don't have the data structure like HashMap, List , TreeList ......  as other language Java,C++ ...

Ask a Question
Discussion stats
  • 3 replies
  • 262 views
  • 2 likes
  • 4 in conversation