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. 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?
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?
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;
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;
yes the second options worked well for mine. Thank you so much!
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.