BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

Dear,

 

I need to create an additional record with sum values of two records. In the below output i need to create the red highlighted record.


data one;
input val$ n1 n2 total;
datalines;
cr 10 12 22 
pr 12 23 35
sd 23 24 47
ne 30 35 65
;

Please suggest.

 

output needed:

val      n1  n2   total

 

cr       10 12 22
pr       12 23 35

cr/pr    22  35  57
sd        23 24 47
ne       30 35 65

4 REPLIES 4
knveraraju91
Barite | Level 11
In my dataset there are only 4 obs. I need to add records with val in(cr,pr) and create new variable cr/pr. Thank you. Please suggest
jimbarbour
Meteorite | Level 14

This isn't particularly elegant, but it will do the job if what you've posted is literally all you want.

data one;
	DROP	_:;
	RETAIN	_CR	0;
	RETAIN	_CR_n1;
	RETAIN	_CR_n2;
	RETAIN	_CR_Total;

	INPUT	val$ n1 n2 total;

	if	 Val			=	'cr'	THEN
		DO;
			_CR			=	1;
			_CR_n1		=	n1;
			_CR_n2		=	n2;
			_CR_Total	=	Total;
		END;

	if	 Val			=	'pr'	THEN
		_PR				=	1;

	IF	_CR	AND	_PR			THEN
		DO;
			OUTPUT;
			Val			=	'cr/pr';
			n1			=	n1	+	_CR_n1;
			n2			=	n2	+	_CR_n2;
			Total		=	Total	+	_CR_Total;
			OUTPUT;
		END;
	ELSE
		DO;
			OUTPUT;
		END;

datalines;
cr 10 12 22 
pr 12 23 35
sd 23 24 47
ne 30 35 65
;
RUN;

Results:

jimbarbour_0-1601528987184.png

 

If that's really all you need, please mark the solution; otherwise, please post more including more extensive data.

 

Jim

 

Kurt_Bremser
Super User
data want:
set have;
_n1 + n1;
_n2 + n2;
output;
if _n_ = 2
then do;
  val = "cr/pr";
  n1 = _n1;
  n2 = _n2;
  total = n1 + n2;
  output;
end;
drop _:;
run;

Untested, posted from my tablet.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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