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
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;
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;
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;
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.
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.