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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 702 views
  • 2 likes
  • 3 in conversation