BookmarkSubscribeRSS Feed
OzoneX15
Calcite | Level 5
Dear Sas members,

I have difficulties in selecting a random sample.
I have to create 1000 portfolios. These portfolios have to pick random stocks with weights (ranging from 0 to 0.05) UNTIL the sum of 1 is reached. Which has to change every time a new portoflio is created.

-->So to be clear:

I have 1 portfolio that has to select a stocks with random weights until the weights sum up to 1 or close to one.

Than again I assign random weights to the stocks and create a second portfolio that randomly chooses stocks until the sums of the weights is 1;.. and so on...

The only column I have are the STOCKS (500 stocks-500 rows)

STOCKS

AN8068571086
ANN4327C1220
ANN6748L1027
AT0000603709
AT0000609607
AT0000620158
AT0000633300
AT0000644505
AT0000652011
AT0000676903
AT0000720008
AT0000729108
AT0000737705
AT0000741053
AT0000743059
AT0000746409
AT0000809058
AT0000821103


I have come up with this code but I am stuck

/*create random weights between 0 and 5 for each stock.*/

data Sample1;
set SASUSER.RANDOM_WEIGHTS;
rand = 1+(5-1)*ranuni(692);
if rand ge 0 and rand le 5;
run;

/*selecting a random sample from a data set is to, first, use a DATA step to generate a random vector,
then use PROC sort to rearrange the data by that random vector and then select first k observations*/


DATA Sample2 ;
SET Sample1 ;
random=RANUNI(-1); /* GENERATE A RANDOM VECTOR */
run;

PROC SORT DATA=Sample2;
BY random; /* SORT OBSERVATIONS BY THE RANDOM VECTOR */
run;

Thanks for helping me out if you know the solution...

Kind regards,

Stefaan Message was edited by: OzoneX15
5 REPLIES 5
darrylovia
Quartz | Level 8
It seems that you're is approach is good and that you are stuck on how to increment to weight up to 1. SAS has a RETAIN feature that retains the value from one record to another. See my below code using an example;




data stocks;
set sashelp.stocks;
weight=ranuni(8)*.05;
rand=ranuni(789);
run;

proc sort data=stocks;
by rand;
run;

data portfolio;
set stocks;
retain one 0;

one+weight;
if one<=1;
run;


Regards
darrylovia
Quartz | Level 8
for some reason my post was truncated.

here is the completed code


data stocks;
set sashelp.stocks;
weight=ranuni(8)*.05;
rand=ranuni(789);
run;

proc sort data=stocks;
by rand;
run;

data portfolio;
set stocks;
retain one 0;

one+weight;
if one le 1;
run;


Regards
Ksharp
Super User
Hi.
Stefaan. It is nice to see you.
How about this:
[pre]



data stock;
infile datalines truncover;
input stock $100.;
datalines;
AN8068571086
ANN4327C1220
ANN6748L1027
AT0000603709
AT0000609607
AT0000620158
AT0000633300
AT0000644505
AT0000652011
AT0000676903
AT0000720008
AT0000729108
AT0000737705
AT0000741053
AT0000743059
AT0000746409
AT0000809058
AT0000821103
AN8068571086
ANN4327C1220
ANN6748L1027
AT0000603709
AT0000609607
AT0000620158
AT0000633300
AT0000644505
AT0000652011
AT0000676903
AT0000720008
AT0000729108
AT0000737705
AT0000741053
AT0000743059
AT0000746409
AT0000809058
AT0000821103
AN8068571086
ANN4327C1220
ANN6748L1027
AT0000603709
AT0000609607
AT0000620158
AT0000633300
AT0000644505
AT0000652011
AT0000676903
AT0000720008
AT0000729108
AT0000737705
AT0000741053
AT0000743059
AT0000746409
AT0000809058
AT0000821103
AN8068571086
ANN4327C1220
ANN6748L1027
AT0000603709
AT0000609607
AT0000620158
AT0000633300
AT0000644505
AT0000652011
AT0000676903
AT0000720008
AT0000729108
AT0000737705
AT0000741053
AT0000743059
AT0000746409
AT0000809058
AT0000821103
AN8068571086
ANN4327C1220
ANN6748L1027
AT0000603709
AT0000609607
AT0000620158
AT0000633300
AT0000644505
AT0000652011
AT0000676903
AT0000720008
AT0000729108
AT0000737705
AT0000741053
AT0000743059
AT0000746409
AT0000809058
AT0000821103
AT0000741053
AT0000743059
AT0000746409
AT0000809058
AT0000821103
AN8068571086
ANN4327C1220
ANN6748L1027
AT0000603709
AT0000609607
AT0000620158
AT0000633300
AT0000644505
AT0000652011
AT0000676903
AT0000720008
AT0000729108
AT0000737705
AT0000741053
AT0000743059
AT0000746409
AT0000809058
AT0000821103
AT0000741053
AT0000743059
AT0000746409
AT0000809058
AT0000821103
AN8068571086
ANN4327C1220
ANN6748L1027
AT0000603709
AT0000609607
AT0000620158
AT0000633300
AT0000644505
AT0000652011
AT0000676903
AT0000720008
AT0000729108
AT0000737705
AT0000741053
AT0000743059
AT0000746409
AT0000809058
AT0000821103
AT0000741053
AT0000743059
AT0000746409
AT0000809058
AT0000821103
AN8068571086
ANN4327C1220
ANN6748L1027
AT0000603709
AT0000609607
AT0000620158
AT0000633300
AT0000644505
AT0000652011
AT0000676903
AT0000720008
AT0000729108
AT0000737705
AT0000741053
AT0000743059
AT0000746409
AT0000809058
AT0000821103
;
run;


%macro port;
%do i=1 %to 1000;
proc surveyselect data=stock method=srs n=100 out=sample&i;
run;

data port&i;
set sample&i;
weight=ranuni(-1)*.05;
sum+weight;
if sum gt 1 then stop;
run;
%end;

data want;
set port: indsname=dsn;
portfolio=scan(dsn,2);
drop dsn;
run;
%mend;

%port




[/pre]

But you need SAS9.2 to run this code . Hope you already have.

Thanks
Ksharp
OzoneX15
Calcite | Level 5
Thanks darrylovia!
and thanks again Ksharp!

Really learning alot from these codes,

Kind regards,

Stefaan Message was edited by: OzoneX15
Patrick
Opal | Level 21
Hi

You can find a lot of sampling samples on the SAS website.

May be the following links are helpful:
http://support.sas.com/rnd/app/da/new/dasurvey.html
http://support.sas.com/kb/?st=11&la=en&qm=3&ct=54042

HTH
Patrick

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1246 views
  • 0 likes
  • 4 in conversation