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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.