BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
abdulla
Pyrite | Level 9
gvkeyfyearTASum (want)
170019953311.6626691.929
170019951599.2346691.929
17001995656.2266691.929
17001995564.766691.929
17001995560.0476691.929
170019964182.83210756.85
170019962068.55410756.85
170019961727.06910756.85
170019961696.43110756.85
170019961081.96310756.85
17001996841.20410756.85
17001996577.84110756.85
17001996.10756.85
8457819953700.925.
8457819951312.852.
8457819961131.176.
8457819961099.786.
8457819961067.566.
845781996848.657.

Here, I want to add TA for the first 5 highest TA. if TA for each year is less than 5 observations,

I want them to be shown as missing sum as in for gvkey 84578. 

Can anyone help me? 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20


data have;
input gvkey	fyear	TA;*	Sum (want);
cards;
1700	1995	3311.662	6691.929
1700	1995	1599.234	6691.929
1700	1995	656.226	6691.929
1700	1995	564.76	6691.929
1700	1995	560.047	6691.929
1700	1996	4182.832	10756.85
1700	1996	2068.554	10756.85
1700	1996	1727.069	10756.85
1700	1996	1696.431	10756.85
1700	1996	1081.963	10756.85
1700	1996	841.204	10756.85
1700	1996	577.841	10756.85
1700	1996	.	10756.85
84578	1995	3700.925	.
84578	1995	1312.852	.
84578	1996	1131.176	.
84578	1996	1099.786	.
84578	1996	1067.566	.
84578	1996	848.657	.
;

proc sort data=have out=_have;
 by gvkey fyear descending ta;
run;

data want;
 do _n_=1 by 1 until(last.fyear);
  set _have;
  by gvkey fyear;
  if _n_<=5 then sum=sum(sum,ta);
 end;
 if _n_<5 then sum=.;
 do _n_=1 to _n_;
  set _have;
  output;
 end;
run;

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20


data have;
input gvkey	fyear	TA;*	Sum (want);
cards;
1700	1995	3311.662	6691.929
1700	1995	1599.234	6691.929
1700	1995	656.226	6691.929
1700	1995	564.76	6691.929
1700	1995	560.047	6691.929
1700	1996	4182.832	10756.85
1700	1996	2068.554	10756.85
1700	1996	1727.069	10756.85
1700	1996	1696.431	10756.85
1700	1996	1081.963	10756.85
1700	1996	841.204	10756.85
1700	1996	577.841	10756.85
1700	1996	.	10756.85
84578	1995	3700.925	.
84578	1995	1312.852	.
84578	1996	1131.176	.
84578	1996	1099.786	.
84578	1996	1067.566	.
84578	1996	848.657	.
;

proc sort data=have out=_have;
 by gvkey fyear descending ta;
run;

data want;
 do _n_=1 by 1 until(last.fyear);
  set _have;
  by gvkey fyear;
  if _n_<=5 then sum=sum(sum,ta);
 end;
 if _n_<5 then sum=.;
 do _n_=1 to _n_;
  set _have;
  output;
 end;
run;

abdulla
Pyrite | Level 9

I have some observations where TA is missing but there more than 5 TA observations. I want them to be missing too. As long as I have 5 given TA observations then I want their sum even if the 6th observation is missing
gvkey fyear ta sum
1000 1996 21.21 .
1000 1996 . .
1000 1996 . .
1000 1996 . .
1000 1996 . .
1000 1996 . .

novinosrin
Tourmaline | Level 20

@abdulla  try-



data have;
input gvkey	fyear	TA;*	Sum (want);
cards;
1700	1995	3311.662	6691.929
1700	1995	1599.234	6691.929
1700	1995	656.226	6691.929
1700	1995	564.76	6691.929
1700	1995	560.047	6691.929
1700	1996	4182.832	10756.85
1700	1996	2068.554	10756.85
1700	1996	1727.069	10756.85
1700	1996	1696.431	10756.85
1700	1996	1081.963	10756.85
1700	1996	841.204	10756.85
1700	1996	577.841	10756.85
1700	1996	.	10756.85
84578	1995	3700.925	.
84578	1995	1312.852	.
84578	1996	1131.176	.
84578	1996	1099.786	.
84578	1996	1067.566	.
84578	1996	848.657	.
1000 1996 21.21 .
1000 1996 . .
1000 1996 . .
1000 1996 . .
1000 1996 . .
1000 1996 . .
;

proc sort data=have out=_have;
 by gvkey fyear descending ta;
run;

data want;
 do _n_=1 by 1 until(last.fyear);
  set _have;
  by gvkey fyear;
  if _n_<=5 then do;
    _n=sum(_n,n(ta));
    sum=sum(sum,ta);
  end;
 end;
 if _n_<5 or _n<5 then sum=.;
 do _n_=1 to _n_;
  set _have;
  output;
 end;
 drop _n;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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