I imported an SPSS (.sav file) into SAS. Several of the variables are not showing up as they are named things like 'variable___1.1' When i try to KEEP certain variables in a data step, I get an error because these variables create an error as SAS misinterprets the '.'
Has anyone encountered this before or know a way around it?
I can see the problem variables and their values in the .sas7bdat file, so the data imported, I just need to find a way to change the variable name so I can include it in the report.
You use name literal notation, 'variable1_1'n in your code, e.g.
rename 'variable___1.1'n = variable1_1;
Or set this option and reimport your data so that you get better names.
option validvarname=v7;
That will tell SAS to import the data with simpler variable names. Note that I'm not sure if that's two underscores or three or four in the variable name....guessing at 3.
Related
I am trying to load SAS data file together with its variable and value labels, but I cant seem to make it work.
I have 3 SAS files
sas data ("data_final.sas7bdat")
sas format dictionary that contains the format name, variable name/labels, etc ("formats.sas7bdat")
sas format library that contains the format name, value name/labels,etc ("format_library.sas7bdat")
I am trying to load this to SPSS using the following code but it doesn't work. It loads the data and the variable labels but not the value labels.
GET SAS DATA='\data_final.sas7bdat'
/FORMATS='\formats.sas7bdat'
/FORMATS='\format_library.sas7bdat'.
Any help is greatly appreciated.
Thank you!
The FORMATS= option wants the name of the SAS format catalog, not another SAS dataset. Catalogs use sas7bcat as the extension.
GET SAS DATA='\data_final.sas7bdat'
/FORMATS='\formats.sas7bcat'.
If you really cannot get it to work then read in the formats_library.sas7bdat and look at the FMTNAME, TYPE, START, END and LABEL variables and use those to generate the SPSS code you need to attach data labels to your SPSS data.
FMTNAME is the name of the format. The TYPE determines if it is applies to character values or numeric values (or if in fact is an INFORMAT instead of FORMAT). The START and END mark the range of values (frequently they will be the same) and LABEL is the decoded value (aka the data label). Unlike in SPSS in SAS you only have to define the code/decode mapping once and then apply to as many variables as you want.
The dataset you show as being named formats.sas7bdat looks like it is the variable level metadata. That should list each variable (NAME) and what format, if any, has been attached to it (FORMAT). So if that shows there is a variable named FRED that has the format YESNO attached to it then look for records in format_library where FMTNAME='YESNO' and see what values it maps. So if FRED is numeric with values 1 and 2 then format YESNO might have one record with START='1' and LABEL='YES' and another with START='2' and LABEL='NO'.
Yesterday I renamed a SAS dataset using my OS. (instead of doing it within a SAS environment). Now, I can see it in my SAS explorer, but when I try to access it SAS tells me that it doesn't exist. I was able to recall the name that I had given the set originally, change it back "manually" and then access it through the interactive SAS environment but, my question:
What the heck is going on here? Is this a flaw in my particular version of SAS or is this just how it is?
Member names in SAS must be valid SAS names. So you couldn't name your dataset 1979data.sas7bdat. You can relax some of the restrictions by setting the option VALIDMEMNAME to EXTEND and using name literals in your SAS code.
But also note that file names on case-sensitive file systems, like Unix, must be all lowercase. So you couldn't use uppercase letters in the name of the file. So SAS would not see a file named OldData.SAS7bdat because if you tried to access a dataset named xxx.OldData it would be looking for a file named olddata.sas7bdat instead.
If a SAS DATA step references a non-existant variable in a DROP, KEEP, or RENAME statement, it returns an error saying such and stops the DATA step due to this error.
How do I get SAS to keep going with the step when it references a non-existent variable? I assume there's an OPTION for this (?) but I can't figure out what it's called if this is the case.
(I'm dealing with yearly datasets for which variables occasionally get added or deleted from year to year.)
Try using:
options dkricond=nowarn dkrocond=nowarn;
First one is for input datasets, second one is for output datasets.
You might want to set these back to warn or error after you are done with the specific data steps where you know this will be an issue.
SAS Manual page
When I load in an excel sheet through PROC IMPORT, locally installed SAS automatically replaces the variable names with spaces to an underscore (_). Such as Patient ID will become Patient_ID and Health Records will become Health_Records.
However, when I load the same file in SAS Studio, that renaming convention isn't applied. So Patient ID and Health Records are kept as is...without the underscore in place.
Thus, how would I call these variables in SAS Studio? A syntax error pops when I try to call IF Patient ID THEN this. Would I have to physically add the underscore to my original dataset or is there an easier way?
The difference is caused by the setting of VALIDVARNAME option.
To refer to variable names with spaces you need to use a name-literal
for example.
"Patient ID"n
Quoted string followed by the letter N.
As #data_null_ notes, VALIDVARNAME=ANY is what is causing this.
If you want SAS Studio to behave like your desktop SAS, simply add
options validvarname=v7;
to the top of your program (or to some program that will run before your imports, like an autoexec). Then your underscores will return.
I have a variable name called A_flag (1=Y,0=N) . However, when I try to rename this variable, SAS says that it can't find the variable.
Is the variable name A_flag (1=Y,0=N) or A_flag ?
If the variablename really is "A_flag (1=Y,0=N)", then you need to refer to it as "A_flag (1=Y,0=N)"n.
E.g:
proc freq;
tables "A_flag (1=Y,0=N)"n;
run;
You need the validvarname=any option set in order for this to work.
That is likely a variable label, not a variable name. If you are in display manager SAS (not Enterprise Guide), go into the View menu when you have a dataset open and select "Column Names" instead of "Column Labels". That will show you the real variable name.
If it is a variable name, it probably came in with Enterprise Guide's default setting of options validvarname=any and a proc import from Excel. If that's the case, I suggest adding to either your configuration file or just to the top line of your program:
options validvarname=v7;
Then run the proc import again and it will properly import the name as a legal SAS variable name without the name literal syntax Stig refers to (which is also a fine solution, but it can be tedious to use that.)