BookmarkSubscribeRSS Feed
shivamarrora0
Obsidian | Level 7

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

 

 s.PNG

 

5 REPLIES 5
art297
Opal | Level 21

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

Reeza
Super User

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

 

 

shivamarrora0
Obsidian | Level 7

Hi Reeza,

 

i know it from proc sql but i want to do it from data step only

vadday9
Calcite | Level 5

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;

 

 


Capture.PNG
art297
Opal | Level 21

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 952 views
  • 3 likes
  • 4 in conversation