BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
3jyager
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

6 REPLIES 6
Reeza
Super User
Show your log please.
ballardw
Super User

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.

3jyager
Calcite | Level 5

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;

ballardw
Super User

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.

Tom
Super User Tom
Super User

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
__________________________________________________________

 

Reeza
Super User

@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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 6 replies
  • 3344 views
  • 1 like
  • 4 in conversation