BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Myurathan
Quartz | Level 8

Hi Experts,

 

I will have to select random entries from a large data set. The condition is sum of their values should be close to a value I choose in the beginning. It is easy to explain with the following test data set.

 

Data test;
input name $ value $;
datalines;
hjk 500
lku 985
ldu 689
lll 951
lkh 147
qwe 653
lkt 566
sads 658
;
run;

I want to randomly select names and their total sum of values should be close to 3000. therefore, I can choose the following names hjk, ldu, qwe, lkt, and, sads. Total summation of their values equal to 3066. 

 

How can I do thi sSAS with a very large data set?

 

Thanks in advance. 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

Hi @Myurathan ,

 

try following step-by-step:

Data test;
input name $ value $;
datalines;
hjk 500
lku 985
ldu 689
lll 951
lkh 147
qwe 653
lkt 566
sads 658
;
run;

data test1;
set test(keep=value) curobs=co;
curobs=co;
call streaminit(123);
r = rand("uniform");
run;
proc sort data = test1;
by r;
run;

data test1(keep=curobs);
set test1;
s+value;
output;
if s > 3000 then 
  do;
    call symputx("NOBS",_N_,"G");
    stop;
  end;
run;

data saple;
  array S[&NOBS.] _temporary_;
  do until(eof);  
    set test1 end = eof;
    _I_ + 1;
    S[_I_] = curobs;
  end;
  drop _I_ curobs;
  call sortn(of S[*]);
  do _I_ = lbound(S) to hbound(S);
    point = S[_I_]; 
    set test point = point;
    output;
  end;
  stop;
run;

All the best

Bart

 

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

2 REPLIES 2
yabwon
Onyx | Level 15

Hi @Myurathan ,

 

try following step-by-step:

Data test;
input name $ value $;
datalines;
hjk 500
lku 985
ldu 689
lll 951
lkh 147
qwe 653
lkt 566
sads 658
;
run;

data test1;
set test(keep=value) curobs=co;
curobs=co;
call streaminit(123);
r = rand("uniform");
run;
proc sort data = test1;
by r;
run;

data test1(keep=curobs);
set test1;
s+value;
output;
if s > 3000 then 
  do;
    call symputx("NOBS",_N_,"G");
    stop;
  end;
run;

data saple;
  array S[&NOBS.] _temporary_;
  do until(eof);  
    set test1 end = eof;
    _I_ + 1;
    S[_I_] = curobs;
  end;
  drop _I_ curobs;
  call sortn(of S[*]);
  do _I_ = lbound(S) to hbound(S);
    point = S[_I_]; 
    set test point = point;
    output;
  end;
  stop;
run;

All the best

Bart

 

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Myurathan
Quartz | Level 8
@yabwon
Thank you so so much. It worked like a charm.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 400 views
  • 0 likes
  • 2 in conversation