BookmarkSubscribeRSS Feed
rishabhmehra13
Calcite | Level 5

Hi,

 

I want a sum of total media value grouped by probability

the data set is already sorted by me

 

data sas1;
 
set d.p3;


by Probability____;
if first.Total_Media_Value=1 then tot_budget=0;
tot_budget+Total_Media_Value;
if last.Total_Media_Value then output;
run;

 

log

NOTE: Variable first.Total_Media_Value is uninitialized.
NOTE: Variable last.Total_Media_Value is uninitialized.
ERROR: BY variables are not properly sorted on data set D.P3.
5 REPLIES 5
Reeza
Super User

Why not use proc means?

 

You can only use FIRST/LAST on variables that are included in your BY statement.

rishabhmehra13
Calcite | Level 5

data sas2;
set d.p3;

by Probability____;
if first.Probability____=1 then tot_budget=0;
tot_budget+Total_Media_Value;
if last.Probability____ then output;

if Probability____=0 then delete;

run;

 

 

still getting the same error

log

 
ERROR: BY variables are not properly sorted on data set D.P3.

 

Reeza
Super User

Then you probably haven't sorted your data.

Sort your data.

rishabhmehra13
Calcite | Level 5
its sorted by probabilty

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Reeza
Super User

Post the Proc sort and data step and the corresponding log that shows the error. Clearly SAS doesn't think it is. 

All values must be ascending for the probability. 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

Register now

Develop Code with SAS Studio

Get started using SAS Studio to write, run and debug your SAS programs.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1937 views
  • 2 likes
  • 2 in conversation