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

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