BookmarkSubscribeRSS Feed
trungcva112
Obsidian | Level 7

Hi everyone. I have an initial dataset like this

data trades; 
input ticker date : monyy7. B S;
format date monyy7.;
 cards;
99999  JAN2001 90 40
99999  FEB2001 40 90
88888  MAY2002 40 70
88888  JUN2002 40 40
 ;
run;

Now I want to create 3 variables: alpha, delta, gamma.

 

 

For each ticker-date, the three variables (alpha, delta, gamma) randomly take values from the set {0.1, 0.3, 0.5, 0.7, 0.9}, one at a time.

 

So for each ticker-date pair, there are 125 combinations of alpha, delta, gamma (there are 5 choices for alpha, followed by 5 choices for delta, followed by 5 choices for gamma ==> total = 5^3 combinations for each ticker-date)

 

So the output dataset is like this

 

Ticker DateBSAlphaDeltaGamma
99999Jan200190400.10.10.1
99999Jan200190400.10.10.3
99999Jan200190400.10.10.5
99999Jan200190400.10.10.7
99999Jan200190400.10.10.9
99999Jan200190400.10.30.1
99999Jan200190400.10.30.3
99999Jan200190400.10.30.5
99999Jan200190400.10.30.7
99999Jan200190400.10.30.9
…..……..……………..…………………….

 

Could anyone please help me how to do this? Thank you very much

5 REPLIES 5
FreelanceReinh
Jade | Level 19

Hi @trungcva112,

 

The dataset containing all combinations (sorted by alpha, delta, gamma within each ticker-date pair) could be created using DO loops:

data want;
set trades;
do alpha=0.1, 0.3, 0.5, 0.7, 0.9;
  do delta=0.1, 0.3, 0.5, 0.7, 0.9;
    do gamma=0.1, 0.3, 0.5, 0.7, 0.9;
      output;
    end;
  end;
end;
run;

To have alpha, delta and gamma randomly take values, a random number function would be applied at some point, but this doesn't seem to be what you want.

PeterClemmensen
Tourmaline | Level 20

I don't understand this request: "For each ticker-date, the three variables (alpha, delta, gamma) randomly take values from the set {0.1, 0.3, 0.5, 0.7, 0.9}, one at a time."

 

Afterwards you want to generate every possible combination of values for alpha, delta and gamma? In that regard, @FreelanceReinhs solution is solid. 

 

If you want to assign values from the specified set {0.1, 0.3, 0.5, 0.7, 0.9} to alpha, delta and gamma, use the RAND Function with the Tables distribution. Some basic examples are given in the article Simulate Categorical Data In SAS.

trungcva112
Obsidian | Level 7

Hi draycut. Yes, I would like to generate all possible combination of values for alpha, delta and gamma. So each ticker-date pair must has 125 combinations, with no duplicate. I will try @FreelanceReinhards solution

Ksharp
Super User

Cartesian Product.

 

data x;
do i=0.1, 0.3, 0.5, 0.7, 0.9;
 output;
end;
run;

data trades; 
input ticker date : monyy7. B S;
n+1;
format date monyy7.;
 cards;
99999  JAN2001 90 40
99999  FEB2001 40 90
88888  MAY2002 40 70
88888  JUN2002 40 40
 ;
run;

proc sql;
create table want as
 select a.*,b.i as alpha,c.i as beta,d.i as gamma
  from trades as a,x as b,x as c,x as d
   order by n,alpha,beta,gamma;
quit;
Kurt_Bremser
Super User

Another way to have fun with it: Use SQL's capabilty of building cartesian joins:

data trades; 
input ticker date : monyy7. B S;
format date monyy7.;
 cards;
99999  JAN2001 90 40
99999  FEB2001 40 90
88888  MAY2002 40 70
88888  JUN2002 40 40
 ;
run;

data source;
input value;
cards;
0.1
0.3
0.5
0.7
0.9
;
run;

proc sql;
create table want as
select * from
  trades,
  source (rename=(value=alpha)),
  source (rename=(value=delta)),
  source (rename=(value=gamma))
;
quit;

 

Edit: expanded the code with dataset trades.

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 662 views
  • 5 likes
  • 5 in conversation