BookmarkSubscribeRSS Feed
purveshrana
Fluorite | Level 6

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

 

 

 

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
Astounding
PROC Star

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;

Haikuo
Onyx | Level 15

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;

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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