BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
michokwu
Quartz | Level 8

Hello,

I want to calculate 5-year average on a time series dataset with Years as the variable names. How do I correctly input the variable names in my code?

 

My data:

Company2001200220032004200520062007200820092010
A9470692035408247817780017047582953886491
B8141851167557427625265518634648957157338
C5069010731832171432699382253340
D102715211319390667110137112012731311

 

What I want

Company20012002200320042005200620072008200920102001-20052006-2010
A94706920354082478177800170475829538864917270.86551.2
B81418511675574276252655186346489571573387417.26945.4
C5069010731832171432699382253340413.8621.2
D102715211319390667110137112012731311984.8851.4

 

Thank you,

Data want;
set have;
'2001-2005'n=mean(of 2001-2005);
'2006-2010'n=mean(of 2006-2010);
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Don't. Use variable labels instead that can be exported instead of names but programming with variable names like that is a recipe for making silly mistakes like adding two raw values together because you forgot the quotes. It's really hard to find those errors. 

 

If you set the following, SAS will import the data with valid SAS names.

 

options validvarname = V7;



If you have to stick with this, for whatever reason, use the same notation you used on the right hand side of the equal sign.

data want;
set have;
Mean_2001_2005 = mean(of '2001'n-'2005'n);
label mean_2001_2005 = "Mean: 2001 to 2005";
run;

proc print data=want LABELS;
run;

@michokwu wrote:

Hello,

I want to calculate 5-year average on a time series dataset with Years as the variable names. How do I correctly input the variable names in my code?

 

My data:

Company 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
A 9470 6920 3540 8247 8177 8001 7047 5829 5388 6491
B 8141 8511 6755 7427 6252 6551 8634 6489 5715 7338
C 506 90 1073 183 217 1432 699 382 253 340
D 1027 1521 1319 390 667 1101 371 1201 273 1311

 

What I want

Company 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2001-2005 2006-2010
A 9470 6920 3540 8247 8177 8001 7047 5829 5388 6491 7270.8 6551.2
B 8141 8511 6755 7427 6252 6551 8634 6489 5715 7338 7417.2 6945.4
C 506 90 1073 183 217 1432 699 382 253 340 413.8 621.2
D 1027 1521 1319 390 667 1101 371 1201 273 1311 984.8 851.4

 

Thank you,

Data want;
set have;
'2001-2005'n=mean(of 2001-2005);
'2006-2010'n=mean(of 2006-2010);
run;

 




View solution in original post

2 REPLIES 2
Reeza
Super User

Don't. Use variable labels instead that can be exported instead of names but programming with variable names like that is a recipe for making silly mistakes like adding two raw values together because you forgot the quotes. It's really hard to find those errors. 

 

If you set the following, SAS will import the data with valid SAS names.

 

options validvarname = V7;



If you have to stick with this, for whatever reason, use the same notation you used on the right hand side of the equal sign.

data want;
set have;
Mean_2001_2005 = mean(of '2001'n-'2005'n);
label mean_2001_2005 = "Mean: 2001 to 2005";
run;

proc print data=want LABELS;
run;

@michokwu wrote:

Hello,

I want to calculate 5-year average on a time series dataset with Years as the variable names. How do I correctly input the variable names in my code?

 

My data:

Company 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
A 9470 6920 3540 8247 8177 8001 7047 5829 5388 6491
B 8141 8511 6755 7427 6252 6551 8634 6489 5715 7338
C 506 90 1073 183 217 1432 699 382 253 340
D 1027 1521 1319 390 667 1101 371 1201 273 1311

 

What I want

Company 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2001-2005 2006-2010
A 9470 6920 3540 8247 8177 8001 7047 5829 5388 6491 7270.8 6551.2
B 8141 8511 6755 7427 6252 6551 8634 6489 5715 7338 7417.2 6945.4
C 506 90 1073 183 217 1432 699 382 253 340 413.8 621.2
D 1027 1521 1319 390 667 1101 371 1201 273 1311 984.8 851.4

 

Thank you,

Data want;
set have;
'2001-2005'n=mean(of 2001-2005);
'2006-2010'n=mean(of 2006-2010);
run;

 




michokwu
Quartz | Level 8

Thank you

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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
  • 2 replies
  • 719 views
  • 2 likes
  • 2 in conversation