Help using Base SAS procedures

Random sample until

Reply
Occasional Contributor
Posts: 14

Random sample until

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
Frequent Contributor
Posts: 139

Re: Random sample until

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
Frequent Contributor
Posts: 139

Re: Random sample until

Posted in reply to darrylovia
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
Super User
Posts: 10,041

Re: Random sample until

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
Occasional Contributor
Posts: 14

Re: Random sample until

Thanks darrylovia!
and thanks again Ksharp!

Really learning alot from these codes,

Kind regards,

Stefaan Message was edited by: OzoneX15
Respected Advisor
Posts: 4,173

Re: Random sample until

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
Ask a Question
Discussion stats
  • 5 replies
  • 328 views
  • 0 likes
  • 4 in conversation