BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ganeshk
Obsidian | Level 7

hi,

i have data set as shown below:

Item_IdCategoryItem_DescriptionDateAGSBCMX
23555212ChipsPotato Lays Chips 50 gms2/1/20152231
23555212ChipsPotato Lays Chips 50 gms3/1/201533
23555212ChipsPotato Lays Chips 50 gms5/1/20152532
23555212ChipsPotato Lays Chips 50 gms6/1/201527
23555212ChipsPotato Lays Chips 50 gms7/1/201522
35442011ChipsChilly Lays Chips 50 gms2/1/201534
35442011ChipsChilly Lays Chips 50 gms3/1/201531
35442011ChipsChilly Lays Chips 50 gms4/1/2015
35442011ChipsChilly Lays Chips 50 gms5/1/201532
35442011ChipsChilly Lays Chips 50 gms6/1/201534

My O/P is to be executed as shown below

Item_IdCategoryItem_DescriptionDateAGSBCMX
23555212ChipsPotato Lays Chips 50 gms2/1/20152231
23555212ChipsPotato Lays Chips 50 gms3/1/2015(24) (i.e. 22+25+27+22/4)33
23555212ChipsPotato Lays Chips 50 gms5/1/20152532
23555212ChipsPotato Lays Chips 50 gms6/1/201527(32) (i.e. 31+33+32/3)
23555212ChipsPotato Lays Chips 50 gms7/1/201522(32) (i.e. 31+33+32/3)
35442011ChipsChilly Lays Chips 50 gms2/1/201534
35442011ChipsChilly Lays Chips 50 gms3/1/201531
35442011ChipsChilly Lays Chips 50 gms4/1/2015(32.75) (i.e. 34+31+32+34/4)
35442011ChipsChilly Lays Chips 50 gms5/1/201532
35442011ChipsChilly Lays Chips 50 gms6/1/201534

How to do this?

Thanks,

Ganesh K

1 ACCEPTED SOLUTION

Accepted Solutions
Steelers_In_DC
Barite | Level 11

Here you go:

DATA have;

infile cards dsd;

informat Item_Id $8. Category $5.    Item_Description $24.    Date mmddyy10.;

format Item_Id $8. Category $5.    Item_Description $24.    Date mmddyy10.;

input Item_Id$    Category$    Item_Description$    Date    AGS    BC    MX;

cards;

23555212,Chips,Potato Lays Chips 50 gms,2/1/2015,22,,31

23555212,Chips,Potato Lays Chips 50 gms,3/1/2015,,,33

23555212,Chips,Potato Lays Chips 50 gms,5/1/2015,25,,32

23555212,Chips,Potato Lays Chips 50 gms,6/1/2015,27,,

23555212,Chips,Potato Lays Chips 50 gms,7/1/2015,22,,

35442011,Chips,Chilly Lays Chips 50 gms,2/1/2015,,34,

35442011,Chips,Chilly Lays Chips 50 gms,3/1/2015,,31,

35442011,Chips,Chilly Lays Chips 50 gms,4/1/2015,,,

35442011,Chips,Chilly Lays Chips 50 gms,5/1/2015,,32,

35442011,Chips,Chilly Lays Chips 50 gms,6/1/2015,,34,

;

run;

proc sql;

create table prep as

select *,avg(ags) as _avg_ags,avg(bc)as _avg_bc,avg(mx) as _avg_mx

from have

group by item_id

order by item_id,date;

data want;

set prep;

by item_id;

if missing(ags) then ags = _avg_ags;

if missing(bc) then bc = _avg_bc;

if missing(mx) then mx = _avg_mx;

drop _:;

run;

View solution in original post

2 REPLIES 2
Steelers_In_DC
Barite | Level 11

Here you go:

DATA have;

infile cards dsd;

informat Item_Id $8. Category $5.    Item_Description $24.    Date mmddyy10.;

format Item_Id $8. Category $5.    Item_Description $24.    Date mmddyy10.;

input Item_Id$    Category$    Item_Description$    Date    AGS    BC    MX;

cards;

23555212,Chips,Potato Lays Chips 50 gms,2/1/2015,22,,31

23555212,Chips,Potato Lays Chips 50 gms,3/1/2015,,,33

23555212,Chips,Potato Lays Chips 50 gms,5/1/2015,25,,32

23555212,Chips,Potato Lays Chips 50 gms,6/1/2015,27,,

23555212,Chips,Potato Lays Chips 50 gms,7/1/2015,22,,

35442011,Chips,Chilly Lays Chips 50 gms,2/1/2015,,34,

35442011,Chips,Chilly Lays Chips 50 gms,3/1/2015,,31,

35442011,Chips,Chilly Lays Chips 50 gms,4/1/2015,,,

35442011,Chips,Chilly Lays Chips 50 gms,5/1/2015,,32,

35442011,Chips,Chilly Lays Chips 50 gms,6/1/2015,,34,

;

run;

proc sql;

create table prep as

select *,avg(ags) as _avg_ags,avg(bc)as _avg_bc,avg(mx) as _avg_mx

from have

group by item_id

order by item_id,date;

data want;

set prep;

by item_id;

if missing(ags) then ags = _avg_ags;

if missing(bc) then bc = _avg_bc;

if missing(mx) then mx = _avg_mx;

drop _:;

run;

Ksharp
Super User

Code: Program

DATA have;
infile cards dsd;
informat Item_Id $8. Category $5.   Item_Description $24.   Date mmddyy10.;
format Item_Id $8. Category $5.   Item_Description $24.   Date mmddyy10.;
input Item_Id$   Category$   Item_Description$   Date   AGS   BC   MX;
cards;
23555212,Chips,Potato Lays Chips 50 gms,2/1/2015,22,,31
23555212,Chips,Potato Lays Chips 50 gms,3/1/2015,,,33
23555212,Chips,Potato Lays Chips 50 gms,5/1/2015,25,,32
23555212,Chips,Potato Lays Chips 50 gms,6/1/2015,27,,
23555212,Chips,Potato Lays Chips 50 gms,7/1/2015,22,,
35442011,Chips,Chilly Lays Chips 50 gms,2/1/2015,,34,
35442011,Chips,Chilly Lays Chips 50 gms,3/1/2015,,31,
35442011,Chips,Chilly Lays Chips 50 gms,4/1/2015,,,
35442011,Chips,Chilly Lays Chips 50 gms,5/1/2015,,32,
35442011,Chips,Chilly Lays Chips 50 gms,6/1/2015,,34,
;
run;

proc sql;
create table prep as
select Item_Id,Category,Item_Description,Date ,
   coalesce(ags,avg(ags)) as ags,
   coalesce(bc,avg(bc)) as bc,
   coalesce(mx,avg(mx)) as mx
from have
group by item_id
order by item_id,date;
quit;

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
  • 2 replies
  • 417 views
  • 3 likes
  • 3 in conversation