Split row into mutiple rows by calculation

Reply
Occasional Contributor
Posts: 5

Split row into mutiple rows by calculation

Geocode   Vol1  Vol2   Vol3...........Vol13

NATION       2        3       5                 4

 

I have single row data entries in this format. I want to add a new colums 'Period'  and 'Current_Vol' for every row 

i.e

if Period = 1- Month then Current_Vol = Vol1

if Period = 3- Month then Current_Vol = Sum(Vol1-Vol3)

if Period = 6 Month then  Current Vol = Sum(Vol1-Vol6)

 

Thus I want display to be as follow

GeoCode     Period                Current_Vol

NATION      1- Month                   2

NATION      3-Month                    10

NATION      6-Month                    X  

 

and so on.

 

Thanks

 

 

 

Super User
Super User
Posts: 9,599

Re: Split row into mutiple rows by calculation

[ Edited ]
Posted in reply to purveshrana

Do refer to the posting guidance when posting a new question, post test data as a datastep, clearly show the problem.  I don't for instance get where period comes in.  Anyways, you almost have the code there:

data have;
Geocode="NATION"; Vol1=2; Vol2=3; Vol3=5; vol4=4; vol5=7; vol6=3;
run;

data want (keep=geocode period current_vol);
set have;
array v{*} vol:;
do i=1,3,6;
period=catx("-",put(i,best.),"Month");
current_vol=sum(v{1}--v{i});
output;
end;
run;
Super User
Posts: 6,785

Re: Split row into mutiple rows by calculation

Posted in reply to purveshrana

Given that you have nearly spelled out the proper statements, it would be easy to fix them and use them in a DATA step:

 

data want;

set have;

Period='1-Month';

Current_Vol = vol1;

output;

period='3-Month';

Current_Vol = sum(of vol1-vol3);

output;

period='6-Month';

Current_Vol = sum(of vol1-vol6);

output;

keep GeoCode Period Current_Vol;

run;

 

proc print data=want;

run;

Respected Advisor
Posts: 3,167

Re: Split row into mutiple rows by calculation

[ Edited ]
Posted in reply to purveshrana

If you data came as is (esp.  your vol variables are following the ascending order, otherwise other steps will be needed), and you want to output every row for each vol,  here is another alternative:

 

data test;
	input Geocode$   Vol1  Vol2   Vol3 Vol13;
	cards;
NATION       2        3       5                 4
;
run;

data want;
	set test;
	array vol vol:;
	length period $ 20;

	do over vol;
		period=catx('-',compress(vname(vol),,'kd'),'Month');
		current_vol+vol;
		output;
	end;

	call missing(current_vol);
	drop vol:;
run;

 

Ask a Question
Discussion stats
  • 3 replies
  • 151 views
  • 2 likes
  • 4 in conversation