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
Amethyst | Level 16

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
Amethyst | Level 16

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 936 views
  • 0 likes
  • 2 in conversation