I am using an existing SAS data set bdims. When I import the data the variable names have a . separating multiple words rather than an _ which is used in the sample codes. When I use the . name it does not recognize the variable.
EX: Knee Diameter should be kne_di but it appears as kne.di
Using SAS on Demand for Academics
Try adding this line before importing the data:
options validvarname=V7;
if you want variables with _ instead of dots.
I don't use the On Demand but it seems that it may be defaulting to Validvarname=any which creates the names with dots. The V7 will enforce the rules for standard SAS variable names which would mean _ in the name.
Run proc contents on the data set.
The text you see may be labels.
If the data set was actually created with non-standard names then you need to set the system option validvarname=any. Which means that to reference to reference the variable name that you must use quotes and an "n" to use the variable in code: 'Some.poor.name'n for example.
I am confused about 'import" though. You don't normally import an existing SAS data set, just use. If you actually ran some proc import code or similar you might show us what you did.
Code is below:
filename bdims url 'http://www.openintro.org/stat/data/bdims.csv';
proc import datafile=bdims
out=bdims
dbms=csv
replace;
getnames=yes;
run;
Try adding this line before importing the data:
options validvarname=V7;
if you want variables with _ instead of dots.
I don't use the On Demand but it seems that it may be defaulting to Validvarname=any which creates the names with dots. The V7 will enforce the rules for standard SAS variable names which would mean _ in the name.
So you don't have a dataset. You have CSV file. If you have the VALIDVARNAME option set to V7 then PROC IMPORT will generate valid SAS names from the header row in the CSV file. But if you have VALIDVARNAME set to ANY then it won't. Instead it will just use the column header as if it was intended to be a variable name.
So either change the option BEFORE the PROC IMPORT step, or get used to using name literals.
For example to reference the first variable in TEST2 in this the example below you would need to use 'COL 1'n. But to reference the first variable in TEST1 you could use COL_1 or 'COL_1'n.
Example:
filename csv temp;
data _null_;
file csv ;
put 'Col 1,Col 2' / '1,2' / '2,3' ;
run;
options validvarname=v7;
proc import datafile=csv dbms=csv out=test1 replace; run;
options validvarname=any ;
proc import datafile=csv dbms=csv out=test2 replace; run;
proc contents data=test1 noprint out=test1_cont; run;
proc contents data=test2 noprint out=test2_cont; run;
proc compare data=test1_cont compare=test2_cont ;
var name label;
run;
Result:
The COMPARE Procedure Comparison of WORK.TEST1_CONT with WORK.TEST2_CONT (Method=EXACT) Variables with Unequal Values Variable Type Len Label Ndif MaxDif NAME CHAR 32 Variable Name 2 Value Comparison Results for Variables __________________________________________________________ || Variable Name || Base Value Compare Value Obs || NAME NAME ________ || ___________________+ ___________________+ || 1 || Col_1 Col 1 2 || Col_2 Col 2 __________________________________________________________
@3jyager wrote:
I am using an existing SAS data set bdims.
The code shown is importing a CSV from a website, not a SAS data set. The code creates a SAS data set based on the CSV.
When I import the data the variable names have a . separating multiple words rather than an _ which is used in the sample codes. When I use the . name it does not recognize the variable.
These variables can be referred to using a slightly different notation, use quotes around the variable name and an N at the end. This is known as a name literal notation.
new_data = 'knee.di'n * 4;
Periods are valid in other variable names in other languages, but not in SAS. To avoid the issue as @ballardw indicated by changing the SAS option to not allow variable names in that style.
option validvarname=v7;
Then it will automatically convert all your variable names to appropriate easily referenced SAS data names, when you import the data.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: