Hi all can you Please solve my small problem
data have;
input id name$ marks;
total+marks;
cards;
1 a 10
2 b 20
3 c 30
4 d 40
;
run;
in the above the last observation of total will 100
i want to make a new variable called V and put the last observation of total in every observation of V
i dont want to hardcore the value i want to make it dynamic
output like this
Here is one way:
data have; input id name$ marks; total+marks; cards; 1 a 10 2 b 20 3 c 30 4 d 40 ; data want; do until (eof1); set have end=eof1; v+marks; end; do until (eof2); set have end=eof2; output; end; run;
Art, CEO, AnalystFinder.com
I'm a fan of SQL for this:
proc sql;
create table want as
select *, sum(marks) as total
from have;
quit;
Another option is to calculate the mean in PROC MEANS and merge in the data:
https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset
Hi Reeza,
i know it from proc sql but i want to do it from data step only
CEO Art297
Thanks for your inputs.
I have used the same code from CEO Art297 and slightly modified to get the expected output.
Shiva,
Can you please check this code.
ata have;
input id name$ marks;
total+marks;
cards;
1 a 10
2 b 20
3 c 30
4 d 40
;
data want;
do until (eof1);
set have end=eof1;
v+marks;
end;
do until (last.marks);
set have ;
output;
end;
run;
proc print;
Why would you change the 2nd dow loop until condition? Yes, in this case it will produce the same result, but with a warning that the variable 'last.marks' has never been initialized.
And, if you included a 'by marks' statement in your revised code it would only produce one record.
I suggest you read up on what a DOW loop is and how it works (see: https://www.google.ca/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=0ahUKEwi1yJWywZXVA... )
Art, CEO, AnalystFinder.com
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.