BookmarkSubscribeRSS Feed
thanikondharish
Calcite | Level 5

data fi ;
input id $ month sale ;
cards ;
111 1 150                                         
111 2 250
112 1 200
112 2 300
112 3 250
;

 111 1 150
111 2 250

111 3 200
112 1 200
112 2 300
112 3 250
;

i want id 111  third month sale as average of first and second month like this  111  3  200

and insert into a dataset

9 REPLIES 9
novinosrin
Tourmaline | Level 20

Hi @thanikondharish do you only have sets of 2 or 3 for each id in your dataset? and is it consistent you want the 3rd month where not available?

thanikondharish
Calcite | Level 5
first id group of third month sale is missing so i want to fill up third
month as average of 1 and 2 month and that record insert into a same dataset
novinosrin
Tourmaline | Level 20
data fi ;
input id $ month sale ;
cards ;
111 1 150                                         
111 2 250
112 1 200
112 2 300
112 3 250
;

data want;
array t(100) _temporary_;
call missing(of t(*));
do _n_=1 by 1 until(last.id);
set fi;
by id month;
t(_n_)=sale;
output;
if last.id and month=2 then do;
month=3;
sale=mean(of t(*));
output;
end;
end;
run;
thanikondharish
Calcite | Level 5
You mentioned month=2 in arrays but in some cases different groups have
different months then what will we do?
novinosrin
Tourmaline | Level 20

Please provide a better and complete sample. 

 

Thats the reason i asked earlier "Hi @thanikondharish do you only have sets of 2 or 3 for each id in your dataset? and is it consistent you want the 3rd month where not available?"

 

You didn't clarify

 

I hope you understood what im asking

thanikondharish
Calcite | Level 5
data s ;
input id $ month sale ;
cards ;
111 1 450
111 2 870
111 3 485
112 1 485
112 2 478
112 3 784
112 4 784
113 1 789
113 2 789
;

i want 4th month sale in every group but in some groups haven't 4th month
sale.So first of all we need to get the average of 1th,2nd,3rd months and
that value assign to 4th month and one more question first off all how to
find which months are missed
s_manoj
Quartz | Level 8
@thanikondharish
As per your wordings, we understand that you need an average for the third month, so month =2 has been mentioned. Please specify your requirement clearly with sample output
thanikondharish
Calcite | Level 5
data s ;
input id $ month sale ;
cards ;
111 1 450
111 2 870
111 3 485
112 1 485
112 2 478
112 3 784
112 4 784
113 1 789
113 2 789
;

i want every 3rd and 4th month sale but in this data only one group has 4th
month so remaining groups haven't 4th month sales . So first of all we get
average of before months and that value assign to 4th month
s_manoj
Quartz | Level 8
data want(drop = n sumsale);
set fi;
by id;
if first.id then do; 
n = 0; 
sumsale = 0;
end;
n +1;
sumsale + sale;
output;
if last.id & month = 2 then do;
month=3;
sale = sumsale/n;
output;
end;
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 9 replies
  • 1529 views
  • 1 like
  • 3 in conversation