BookmarkSubscribeRSS Feed
ejohnson96
Calcite | Level 5

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.

3 REPLIES 3
Reeza
Super User

It's probably better to use SAS functionality. 

 

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

Rick_SAS
SAS Super FREQ

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.

Ksharp
Super User
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++ ...

sas-innovate-white.png

🚨 Early Bird Rate Extended!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Lock in the best rate now before the price increases on April 1.

Register now!

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 3 replies
  • 1383 views
  • 2 likes
  • 4 in conversation