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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

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