SAS: Conditional choice of library - sas

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.

Related

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 view permanent inputs/outputs of a project or program

Is there any way to view all permanently stored datasets used by or created by a SAS project (or program, if this is not possible)? I have been tasked with creating a matrix of data inputs and outputs for 40 different SAS projects, each of which contains at least 50 programs. Needless to say there are THOUSANDS of temporary datasets created, but all I am interested in are the permanent ones. After manually checking one project, I noticed that the project process flow does not contain many permanently stored inputs (i.e. from libraries other than WORK) and it is very time consuming to check the properties of each dataset to see if it is temporary or not.
Three other things of note-
1. None of the code is documented.
2. I did not write any of it.
3. I am using SAS enterprise guide
it is not exactly clear what you are asking for. You may want to check out the table sashelp.Vcolumn which will contain a list of all permanent data sets and lists variables of each table. If your project stored all data sets in one library you could try:
proc sql;
select * from sashelp.vcolumn
where libname ="yourprojectslibrary";

Variable Name in 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.)

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.

SAS: Is it possible to store a compiled macro in a catalog other than sasmacr?

SAS macros are compiled and stored into a catalog named sasmacr. Is there a way to store them in a catalog with a different name?
The use case is a test harness where I wish to create a collection of test case macros in a namespace where they cannot clash with macros defined in the program being tested. My workaround is to prefix all my test macros' names with what I believe is a unique string.
From the SAS Site
libname mymacs 'SAS-data-library';
filename mymacros catalog 'mymacs.myautos';
You can by using the system options MAUTOSOURCE and SASAUTOS to direct SAS where to search for stored macros.