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

There are two observations and I would like to change the variable "total" in the first observation to "55" without hardcoding

JeeAnTaeOo_0-1609615226241.png

data final;
set overall;
total=n;
total+lag(n);
prpl=n/total;
run;

 

The code above is what I used to generate the table

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Looks like you are trying to generate TOTAL, not move it from one observation to another.

You could just calculate the total and then combine it back onto the data set.

proc summary data=overall;
  var n;
  output out=total sum=total;
run;

data final;
  set overall;
  if _n_=1 then set total(keep=total);
  prpl=n/total;
run;

Here is a way to do it in one step:

data final;
  do until(eof1);
     set overall end=eof1;
     total+n;
  end;
  do until(eof2);
    set overall end=eof2;
    prpl=n/total;
    output;
  end;
run;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

You need a "look-ahead":

data final;
merge
  overall
  overall (
    firstobs=2
    keep=n
    rename=(n=_n)
  )
;
total = sum(n,_n);
run;

Untested; for tested code, provide your data as a data step with datalines.

Tom
Super User Tom
Super User

Looks like you are trying to generate TOTAL, not move it from one observation to another.

You could just calculate the total and then combine it back onto the data set.

proc summary data=overall;
  var n;
  output out=total sum=total;
run;

data final;
  set overall;
  if _n_=1 then set total(keep=total);
  prpl=n/total;
run;

Here is a way to do it in one step:

data final;
  do until(eof1);
     set overall end=eof1;
     total+n;
  end;
  do until(eof2);
    set overall end=eof2;
    prpl=n/total;
    output;
  end;
run;

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 684 views
  • 0 likes
  • 3 in conversation