libname hw5a "C:\Users\noor\Desktop";
data hw5a;
set hw5a.HW5a;
by week;
if first.Week then cumNsurgery= Nsurgery;
else cumNsurgery + Nsurgery;
keep;
run;
I am trying to create a cumulative but whats wrong with my code
my data is
week Nsurgery
1 1
2 0
3 5
4 7
5 4
6 0
7 1
8 3
9 5
Could you please try the below code, i used the retain statement
data hw5a;
set hw5a.HW5a;
by week;
retain cumNsurgery;
if first.Week then cumNsurgery= Nsurgery;
else cumNsurgery + Nsurgery;
keep;
run;
What's the problem?
I am getting same reading as neurosurgery its not giving me cummulative
do I have to place retain some where
Could you please try the below code, i used the retain statement
data hw5a;
set hw5a.HW5a;
by week;
retain cumNsurgery;
if first.Week then cumNsurgery= Nsurgery;
else cumNsurgery + Nsurgery;
keep;
run;
@Jagadishkatam wrote:
Could you please try the below code, i used the retain statement
data hw5a; set hw5a.HW5a; by week; retain cumNsurgery; if first.Week then cumNsurgery= Nsurgery; else cumNsurgery + Nsurgery; keep; run;
As far as I know, the RETAIN statement shouldn't make the slightest difference here. Can someone explain why it matters in this example?
I think the retain is required because comNsurgery is already in dataset hw5a. In that case, the sum statement being after the set statement does not imply retain.
@ambreen wrote:
libname hw5a "C:\Users\noor\Desktop";
data hw5a;
set hw5a.HW5a;
by week;
if first.Week then cumNsurgery= Nsurgery;
else cumNsurgery + Nsurgery;
keep;
run;
I am trying to create a cumulative but whats wrong with my code
Please post the whole log of that code. Use the {I} button for posting the log.
how can I create a cumulative variable cumN week that shows cumulative week with atleast one surgery
@ambreen wrote:
how can I create a cumulative variable cumN week that shows cumulative week with atleast one surgery
Show us a portion of the data you are using, following these instructions: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...
I doubt we can come up with the right answer if we can't see your data.
@ambreen wrote:
how can I create a cumulative variable cumN week that shows cumulative week with atleast one surgery
Please read my previous post again.
@ambreen wrote:
libname hw5a "C:\Users\noor\Desktop";
data hw5a;
set hw5a.HW5a;
by week;
if first.Week then cumNsurgery= Nsurgery;
else cumNsurgery + Nsurgery;
keep;
run;
If you are getting the same value, perhaps it is Nsurgery that does not have the value you expect. Have you looked at it?
Nevertheless, don't expect us to guess what is happening; show us a portion of your data, following these instructions: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...
Hi @ambreen It's my humble request that you pay attention to @PGStats and @PaigeMiller as the solution doesn't quite relate to the actual problem. Your code seems ok with or without redundant RETAIN considering you have used a SUM statement which is implicit retain. Therefore, that cannot be the fix. I'm afraid if you miss out on those intricate details, your result might not be what you want downstream. Well, it's up to you.
Agree that the RETAIN statement does nothing. Some other possibilities:
cumNSurgery + NSurgery;
At any rate, this statement should be removed:
keep;
@ambreen wrote:
libname hw5a "C:\Users\noor\Desktop";
data hw5a;
set hw5a.HW5a;
by week;
if first.Week then cumNsurgery= Nsurgery;
else cumNsurgery + Nsurgery;
keep;
run;
I am trying to create a cumulative but whats wrong with my code
my data is
week Nsurgery
1 1
2 0
3 5
4 7
5 4
6 0
7 1
8 3
9 5
Your shown data and the attached data set only have one record with each "week" value. So every record is the first week and there is only the one nsurgery value.
If you have data with multiple records for week then share.
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.