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

Hi there!

 

Consider the following problem:

 

TableA:

1.png

 

 

 

 

 

 

 

 

I would like to select the first rows that the sum of values is equal 10. Therefore, the output is:

 

2.png

 

 

 

 

 

 

Someone know how to do this in DATA SET or PROC SQL?

 

Thanks 🙂

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

Like this?

data HAVE;
input ID VALUE;
datalines;
1 3
2 5
4 14
5 5
run;
data WANT;
  set HAVE;
  SUM+VALUE;
  if SUM>=10 then do;
    VALUE+-SUM+10;
    output; 
    stop;
  end;
  else output;
run;

 

ID VALUE SUM
1 3 3
2 5 8
4 2 22

 

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20
data have;
input id value;
datalines;
1 3
2 5
3 2
4 14
5 5
;

data _null_;
if _N_ = 1 then do;
	if 0 then set have;
   declare hash h(dataset:'have(obs=0)',ordered:'y');
   h.defineKey('id');
   h.defineData('id','value');
   h.defineDone();
end;
set have;
_value+value;
h.add();
if _value>=10 then do;h.output(dataset:'want');stop;end;
run;
RoberJunior
Fluorite | Level 6
We change the last row value. The output will be:
1
1
8
ChrisNZ
Tourmaline | Level 20

Like this?

data HAVE;
input ID VALUE;
datalines;
1 3
2 5
4 14
5 5
run;
data WANT;
  set HAVE;
  SUM+VALUE;
  if SUM>=10 then do;
    VALUE+-SUM+10;
    output; 
    stop;
  end;
  else output;
run;

 

ID VALUE SUM
1 3 3
2 5 8
4 2 22

 

novinosrin
Tourmaline | Level 20
data _null_;
if _N_ = 1 then do;
	if 0 then set have;
   declare hash h(dataset:'have(obs=0)',ordered:'y');
   h.defineKey('id');
   h.defineData('id','value');
   h.defineDone();
end;
set have;
_k=_value;_value+value;
if _value<=10 then h.add();
else if _value=10 then do;h.output(dataset:'want');stop;end;
else if _value>10 then do;value=value-_k-1;h.add();h.output(dataset:'want');stop;end;
run;
RoberJunior
Fluorite | Level 6

Thanks for help me guys 🙂

 

Now, I want to develop a new step. Consider this example:

 

TABLE 01:
1.png

 

 

 

 

 

 

 

 

 

 

I want the declare a variable X that is sum VALUES if CLASS = A. Therefore, X = 10

 

Then will run the code with variable X:

 

data _null_;
if _N_ = 1 then do;
	if 0 then set have;
   declare hash h(dataset:'have(obs=0)',ordered:'y');
   h.defineKey('id');
   h.defineData('id','value');
   h.defineDone();
end;
set have;
_k=_value;_value+value;
if _value<=X then h.add();
else if _value=X then do;h.output(dataset:'want');stop;end;
else if _value>X then do;value=value-_k-1;h.add();h.output(dataset:'want');stop;end;
run;

 

Can you guys help to develop the first step, plz?

 

Thanks 🙂

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
  • 6 replies
  • 1037 views
  • 3 likes
  • 3 in conversation