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

I have a dataset (have) with columns named as follows: time_period, vol_-1, vol_0, vol_1, vol_1000.

I want to create a column that calculates the sum of these values:

data want;
set have;
total_vol = sum(vol_-1, vol_0, vol_1, vol_1000);
run;

However, since vol_-1 has a special character in it, I am getting an error. How can I get around this problem?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

1. Re-import your data after specifying the following:

options validvarname=v7;
...
import and rest of code;
...


total_vol = sum(vol__1, vol_0, vol_1, vol_1000);

This will ensure that SAS imports the data with names that are easier to type - ie it will convert symbols to underscores so your variable will likely be vol__1 -> which could easily be mistaken with vol_1. 

 

2. Use the name literal version to refer to a data set or variable name that has spaces or special characters.

total_vol = sum('vol_-1'n, vol_0, vol_1, vol_1000);

@aalluru wrote:

I have a dataset (have) with columns named as follows: time_period, vol_-1, vol_0, vol_1, vol_1000.

I want to create a column that calculates the sum of these values:

data want;
set have;
total_vol = sum(vol_-1, vol_0, vol_1, vol_1000);
run;

However, since vol_-1 has a special character in it, I am getting an error. How can I get around this problem?


 

View solution in original post

4 REPLIES 4
Reeza
Super User

1. Re-import your data after specifying the following:

options validvarname=v7;
...
import and rest of code;
...


total_vol = sum(vol__1, vol_0, vol_1, vol_1000);

This will ensure that SAS imports the data with names that are easier to type - ie it will convert symbols to underscores so your variable will likely be vol__1 -> which could easily be mistaken with vol_1. 

 

2. Use the name literal version to refer to a data set or variable name that has spaces or special characters.

total_vol = sum('vol_-1'n, vol_0, vol_1, vol_1000);

@aalluru wrote:

I have a dataset (have) with columns named as follows: time_period, vol_-1, vol_0, vol_1, vol_1000.

I want to create a column that calculates the sum of these values:

data want;
set have;
total_vol = sum(vol_-1, vol_0, vol_1, vol_1000);
run;

However, since vol_-1 has a special character in it, I am getting an error. How can I get around this problem?


 

aalluru
Obsidian | Level 7

I did not import the data, I created the dataset. 

data data;
	set info_raw;
	if <condition> then discount_grp = -1;
	else if <condition> then discount_grp = 0;
	else if <condition> then discount_grp = 1;
	else if <condition> then discount_grp = 1000;
.........
run;

After that I did some more manipulations and used the proc transpose procedure to create the have table and that seemed to work which is why I the columns got named that way

proc transpose data=freq2 out=freq3 (DROP=_LABEL_ _NAME_ ) prefix=vol_;
by time_period;
id disc_grp;
var frequency;
run;
Reeza
Super User

The concept is the same, did neither of the suggested options work for you?

 


@aalluru wrote:

I did not import the data, I created the dataset. 

data data;
	set info_raw;
	if <condition> then discount_grp = -1;
	else if <condition> then discount_grp = 0;
	else if <condition> then discount_grp = 1;
	else if <condition> then discount_grp = 1000;
.........
run;

After that I did some more manipulations and used the proc transpose procedure to create the have table and that seemed to work which is why I the columns got named that way

proc transpose data=freq2 out=freq3 (DROP=_LABEL_ _NAME_ ) prefix=vol_;
by time_period;
id disc_grp;
var frequency;
run;

 

 

aalluru
Obsidian | Level 7

yes the second options worked well for mine. Thank you so much!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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