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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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;

 

 

Thanks,
Jag

View solution in original post

13 REPLIES 13
PGStats
Opal | Level 21

What's the problem?

PG
ambreen
Calcite | Level 5

I am getting same reading as neurosurgery its not giving me cummulative

do I have to place retain some where

Jagadishkatam
Amethyst | Level 16

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;

 

 

Thanks,
Jag
PaigeMiller
Diamond | Level 26

@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?

--
Paige Miller
PGStats
Opal | Level 21

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.

PG
Kurt_Bremser
Super User

@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.

ambreen
Calcite | Level 5

how can I create a cumulative variable cumN week that shows cumulative week with atleast one surgery

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
Kurt_Bremser
Super User

@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.

PaigeMiller
Diamond | Level 26

@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...

--
Paige Miller
novinosrin
Tourmaline | Level 20

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. 

Astounding
PROC Star

Agree that the RETAIN statement does nothing.  Some other possibilities:

 

  • If cumNSurgery is already part of the incoming data set, then it gets replaced each time the SET statement executes.  That's every time.  Perhaps it is already in the data with a value of 0.
  • Perhaps WEEK is the wrong variable to use in the BY statement.  The results you are claiming to get should occur if your data has just one observation for each WEEK.  That seems to be the case with the data that you posted, so you should expect cumNSurgery to equal NSurgery when there is just a a single observation per week.  Perhaps the IF THEN logic should be replaced with simply:

cumNSurgery + NSurgery;

 

At any rate, this statement should be removed:

 

keep;

 

ballardw
Super User

@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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 13 replies
  • 1104 views
  • 1 like
  • 8 in conversation