Hi,
I have a dataset with a single obs, which is like:
The num here is computed cumulatively based on another column ( sum of the value in that column)
The condition defined as whether the num is smaller than a specific value.
If yes, then I need to append a new dataset to this dataset and compute the cumulative value (including the "num" here) again until it meets the condition, then I can extract this obs into a dataset with a single value.
If not, I just want to keep the dataset.
The code I use:
data want;
set have;
if value <= 10000000 then do;
set new.have;
by name;
if first.obs then do;
value = value;end;
value + value1;
else if value > 10000000 then output;
run;
I am quite new to SAS, so any helps are much appreciated!
Thank you very much!
Let's say you have a second data set named MORE_VALUES, that contains a variable named VALUE1. You could approach the problem in this way:
data want;
set have;
if value <= 10000000 then do until (value > 10000000);
set more_values;
value + value1;
end;
output;
stop;
keep name value;
run;
Let's say you have a second data set named MORE_VALUES, that contains a variable named VALUE1. You could approach the problem in this way:
data want;
set have;
if value <= 10000000 then do until (value > 10000000);
set more_values;
value + value1;
end;
output;
stop;
keep name value;
run;
I'm going to suggest a slightly different approach that isn't going to involve creating what I suspect would end up being a maintenance nightmare if you have to do this repetitively:
Combine multiple data sets
Calculate a rolling cumulative for all of the data.
Select the data where the value of the cumulative value is less than or equal to your threshold value.
/* create two datasets just to have something to demostrate*/ data work.one; do x= 1 to 20; output; end; run; data work.two; do x= 5 to 25; output; end; run; data work.both; set work.one work.two ; retain cumx; cumx = sum(cumx,x); /* if the threshold value of the cumulative variable is 305*/ output; if cumx ge 305 then stop; run;
You may have to work on additional logic if you want the data where the cumulative value is only less than the threshold but doesn't exceed it.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.