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.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.