I would like to export all available user defined formats into a single lookup table that would contain columns format, value, and label (so that I can use it to manipulate data in R and SQL).
Is there a way to do this ?
You can export the contents of a SAS format catalog (commonly, formats.sas7bcat) with the CNTLOUT method on PROC FORMAT:
proc format lib=mylib cntlout=myformatds;
quit;
This would take the default format catalog stored in the library mylib as formats.sas7bcat and export it to a dataset, myformatds in the work library.
As #user667489 indicated in the comment, the answer was in using PROC FORMAT with the CNTLOUT option.
The general format of the instruction is:
PROC FORMAT
LIBRARY=lib_name CNTLOUT=created_table;
RUN;
One must specify the library however, to find out which library is relevant one can take a look at SASHELP.VFORMAT.
Then one can create different tables and concatenate them.
In my case all user defined formats were in a library called LIBRARY and I don't need all columns so my solution is:
PROC FORMAT
LIBRARY=LIBRARY CNTLOUT=FORMAT_LOOKUP (KEEP=FMTNAME START END LABEL);
RUN;
Related
I have a requirement to change formats.sas7bcat file to forms.sas7bcat
I am wondering, if it is possible? If yes, then how should I do that?
Either manually or with coding?
Thanks for your Help!
You can use PROC DATASETS to rename catalogs in addition to renaming datasets. To make sure it finds the catalog named FORMATS instead of a dataset named FORMATS use the MEMTYPE= option.
So if the catalog is in the libref MYLIB then the code would look like:
proc datasets nolist lib=MYLIB memtype=catalog;
change formats=forms ;
run;
quit;
The formats.sas7bcat file is a file on disk, so you certainly could rename it using windows techniques (or linux or whatnot).
You also can create it with the desired name when you create your formats, by using:
proc format library=yourlib.forms;
[whatever format code you are using]
run;
Then you can use options fmtsearch=(yourlib.forms work); or similar to make sure it is looking at that format catalog for formats.
Say I have the a library named mylib.
Within the mylib library, the following datasets are held:
mylib.data_yearly_2015
mylib.data_yearly_2016
mylib.data_yearly_2017
mylib.data_yearly_2018
mylib.data_yearly_2015
mylib.data_mtly_01JUN2015
mylib.data_mtly_01DEC2015
mylib.data_mtly_01JUN2016
mylib.data_mtly_01DEC2016
mylib.data_mtly_01JUN2017
mylib.data_mtly_01DEC2017
Now I need to write a macro that will specifically choose the latest data_mtly_xxxxxx table from the mylib library.
For example, in the current stage, it should choose mylib.data_mtly_01DEC2017
If, however, a new dataset gets added, for example mylib.data_mtly_01JUN2018, it would have to choose that table.
How can I go about doing this in SAS?
Get a list of all data sets
Get the date portion using SCAN() and INPUT()
Get max date.
Proc sql noprint;
Select max(input(scan(name, -1, ‘_’), date9.) ) into :latest_date
From sashelp.vtable
Where upcase(libname) = ‘MYLIB’ and upcase(memname) like ‘DATA_MTLY_%’;
Quit;
Now you should have the latest date value in a macro variable and can use that in your code.
%put &latest_date.;
If it looks like a number and not a date, you’ll need a format applied but you should be able to convert it using PUT().
Note: code is untested.
I have few datasets in SAS. They are to be exported to a excel file in some locations. Each dataset to be exported to some range(named ranges are defined in that excel). Is there any possibility to export datasets into excel for a specific "named ranges" which are predefined in existing excel file.
Thanks,
Ravi
There is a solution to export directly to an Excel named range provided you have SAS/ACCESS to Excel. For this example, assume that you have Office installed in your SAS environment, the Excel file is stored in C:\Data, and you have a named range called Named_Range.
First, you want to make a connection to the Excel data set using the libname engine:
libname xls Excel 'C:\Data\Excel_Data.xlsx';
You can then output directly to the named range like a data set. If there are contents already in it, be sure to delete them first using PROC DATASETS.
proc datasets lib=xls nolist;
delete Named_Range;
quit;
data xls.Named_Range;
set have;
run;
This is just one example of using it, but the thing that's so great about the libname engine is how it takes external data sets and lets you treat them like SAS data sets. If your named range had the right dimensions, you could theoretically output directly to it using any procedure that can produce a data set.
If you do not have SAS/ACCESS to Excel, I am unsure of a solution specifically with named ranges, though you could work around it using the RANGE= option in PROC EXPORT with the xls/xlsx dbms type.
Sources
http://www.stratia.ca/papers/excel_libname.pdf
http://support.sas.com/documentation/cdl/en/acpcref/63181/HTML/default/viewer.htm#n1wvmggexroxgyn17rp61jml3cvn.htm
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;
In the following sas code segment, what do the "lib=sasuser " and "select" mean here? Thanks.
proc format lib=sasuser cntlout = sasuser.fmtdata;
select $airport;
run;
The code you posted will read the formats catalog from the sasuser library (sasuser.formats), selecting only the custom character format $airport (out of the many other possible formats that might also be contained in the catalog) and write its definition to a SAS table (sasuser.fmtdata).
Once the format definition is in a SAS table you can use it to generate a report on the definition of the format. You can also modify the definition of the format by modifying the table contents and feeding it back into proc format using the cntlin option to create or update a format in a format catalog by reading its definition from an input SAS table.
You can find out more about the various proc format options on the SAS support site at http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/a000063536.htm