Variable Name in SAS - sas

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

Related

Variable names incompatible between SPSS and SAS

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.

SAS data set "nonexistent"?

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.

SAS: Conditional choice of library

I have the same table name in two different libraries, and I want to use a condition in the beginning to choose what library to use (SAS Enterprise Guide). Is it possible to use a variable for the library to achieve something like below?
IF(txt = 'tst")
Select * from TST.TableName
ELSE
Select * form DEV.TableName
The best way to do this varies based on what defines txt. If you are choosing this based on user input (in your example, user chooses TEST or DEV (or PROD) database to point to), then the best way in Enterprise Guide is to create a prompt to create a macro variable, or secondarily directly create a macro variable.
Ben Cochran's paper Be Prompt Now: Creating and Using Prompts in SAS Enterprise Guide is a good introduction to the topic, as well as other sources online. You can set up a prompt that asks the user which environment to point to, and then it will define a macro variable with a value of your choosing. Then add the prompt to the program that you need it to be related to, and presto, it works.
So in your case, you could have it set a variable &env. that contains the value of the table name (TST or DEV). You could also have it control the libname value itself (so you don't change which libname is used, but you instead change which folder or database the libname points to).
If you're not in EG, or if you don't want to use a prompt for this for whatever reason, you similarly can assign that macro variable yourself.
%let env = TST;
Either way, once you've got &ENV=TST established, you just use it in open code:
proc sql;
select * from &ENV..TableName;
quit;
Or, like I said, you could assign the libname based on the macro variable and use a single libname in code; that's often cleaner, though it leaves its own complications.

IGNORE DATA IN SAS IMPORT FROM EXCEL

I have no working knowledge of SAS, but I have an excel file that I need to import and work with. In the excel file there are about 100 rows (observations) and 7 columns (quantities). In some cases, a particular observation may not have any data in one column. I need to completely ignore that observation when reading my data into SAS. I'm wondering what the commands for this would be.
An obvious cheap solution would be to delete the rows in the excel file with missing data, but I want to do this with SAS commands, because I want to learn some SAS.
Thanks!
Import the data however you want, for example with the IMPORT procedure, as Stig Eide mentioned.
proc import
datafile = 'C:\...\file.xlsx'
dbms = xlsx
out = xldata
replace;
mixed = YES;
getnames = YES;
run;
Explanation:
The DBMS= option specifies how SAS will try to read the data. If your file is an Excel 2007+ file, i.e. xlsx, then you can use DBMS=XLSX as shown here. If your file is older, e.g. xls rather than xlsx, try DBMS=EXCEL.
The OUT= option names the output dataset.
If a single level name is specified, the dataset is written to the WORK library. That's the temporary library that's unique to each SAS session. It gets deleted when the session ends.
To create a permanent dataset, specify a two level name, like mylib.xldata, where mylib refers to a SAS library reference (libref) created with a LIBNAME statement.
REPLACE replaces the dataset created the first time you run this step.
MIXED=YES tells SAS that the data may be of mixed types.
GETNAMES=YES will name your SAS dataset variables based on the column names in Excel.
If I understand you correctly, you want to remove every observation in the dataset that has a missing value in any of the seven columns. There are fancier ways to do this, but I recommend a simple approach like this:
data xldata;
set xldata;
where cmiss(col1, col2, ..., col7) = 0;
run;
The CMISS function counts the number of missing values in the variables you specify at each observation, regardless of the data type. Since we're using WHERE CMISS()=0, the resulting dataset will contain only the records with no missing data for any of the seven columns.
When in doubt, try browsing the SAS online documentation. It's very thorough.
If you have "SAS/ACCESS Interface to PC Files" licensed (hint: proc setinit) you can import the Excel file with this code. The where option lets you select which rows you want to keep, in this example you will keep the rows where the column "name" is not blank:
proc import
DATAFILE="your file.xlsx"
DBMS=XLSX
OUT=resulttabel(where=(name ne ""))
REPLACE;
MIXED=YES;
QUIT;

Rename Variable Name Starting with Number in SAS

I have some results that came from a relational database in a SAS data set. All of the variable names start with numbers, so I can't rename them or access them in a data step. Is there any way to rename them or access them without getting the data out of the RDBMS again?
options validvarname=any; will allow you to access them, and perhaps even use the dataset - you can enclose an "illegal" variable name in "variable name"n (quotes then an n afterwards) to make a name literal which is equivalent to a variable name (like in Oracle using "variable name").
If you want to make them easier to use, you can do something like
proc sql;
select catx(' ','rename',name,'=',cats('_',name,';')) into :renamelist separated by ' '
from dictionary.columns
where libname='WORK' and memname='DATASETNAME'; *perhaps AND ANYDIGIT(substr(name,1,1)) as well;
quit;
proc datasets lib=work;
modify datasetname;
&renamelist;
quit;
You could also try setting options validvarname=v7; before you connect to the RDBMS as it's possible SAS will do this for you (depending on the situation) if you have it set that way (and don't currently).
The answer given by Joe has some helpful information, but I actually discovered that SAS has a (somewhat automatic) method for handling this. When you query data from an RDBMS, SAS will actually replace any column names starting with numbers with an underscore for the first character. So 1994Q4 becomes _994Q4. Thus, you can simply access the data that way.
SAS will, however, preserve the original name from the RDBMS as the variable title, so it will display as 1994Q4 (or whatever) in table view mode.