Exporting sas dataset to excel named ranges - sas

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

Related

Can we rename formats Catalog in sas?

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.

Export .sas7bdat from SAS Studio to local machine

I am using SAS Studio(completely browser based). I need to export a dataset to my local machine in the .sas7bdat file format. I think it should be something like PROC EXPORT data = sqrtReg2 outfile = "C:\Documents\SAS\Target_Wins.sas7bdat";. But that returns the error ERROR: Unable to determine datasource type. Please use the DBMS= option.. But the DBMS option only allows for CSV, tab and DLM. How do I export this data set to my local machine in the .sas7bdat file format?
With the SAS University Edition you can setup shared folders in the virtual machine where SAS runs that are mapped to actual folders on your real machine.
For example you might have mapped C:\Documents\SAS\ to /folders/myfolders. You cannot write to other locations on your real machine that are not mapped so that the virtual machine can see them. Check the documentation for exact details of getting the folders mapped.
The normal way to have SAS place a dataset then is to create a libref that points to the folder and then use a two level name when referencing the data set. You could create a libref named OUT for example:
libname out '/folders/myfolders/';
data out.target_wins;
set sqrtReg2;
run;
But you can also just refer to the file directly without first creating a libref.
data '/folders/myfolders/target_wins';
set sqrtReg2;
run;
Note that since SAS is actually running in Unix you cannot use CamelCaseFileNames for your SAS datasets. The files will always be in all lowercase letters.
None of the answers worked for me. Maybe because after April 2021, they have made changes to the platform(University). So, after a lot a time searching, I found what I needed.
You can easily export the sas dataset to csv, xslx, by just right clicking on the dataset and selecting export as csv, xlsx, etc.
For exporting to sas7bdat file, do:
Create your dataset, I am creating from csv, so create a program1(.sas) to first convert csv to sas dataset.
proc import file="/home/u123/mydata.csv"
out=work.mydata
dbms=csv
replace;
run;
This will create your sas dataset.
IMP Go to "Libraries" at the right bottom, and hit "My Libraries" -> New Library -> Name it(eg - test), give path(eg - /home/u123/sasuser.v94)
Check library creation, and HIT "Refresh Library Session" on right pane, don't refresh the page.
Now create a separate program2(.sas), to export the dataset to .sas7dbat file.
PROC COPY IN=WORK OUT=test;
SELECT mydata;
run;
quit
This will create a .sas7bdat file in your directory, with same name as your dataset.
These exact steps worked out for me.
It won't work for 2 reasons.
You can't export a SAS dataset to a SAS dataset (.sas7bdat) - Proc Export will export to excel, csv, etc but not to a .sas7bdat.
you're running SAS Studio from within a Virtual Machine that uses Linux as OS so path to create an external file is incorrect. You haven't hit this error but you will once you use the right filetype.
When you installed SAS Studio you should have created a shared folder. This folder is accessed from within SAS Studio as /folders/myfolders/filename.
So your code should looks like this:
PROC EXPORT data = sqrtReg2
outfile = "/folders/myfolders/Target_Wins.csv";
run;
From Windows the path to this shared folder will depend upon where you installed your VM.
Option 2
If what you need is the dataset then try the following code:
libname out "/folders/myfolders/";
proc copy in=work out=out;
select sqrtReg2 ;
run;
Again, table will be in your shared folder which is accesible from Windows.
I used just like option 2 in the above answer. I need to export my polygon data from SAS Studio in my virtual machine so that i can import it to my Visual Analytics. and it worked just fine.
libname out "/folders/myfolders/";
proc copy in=work out=out;
select my_map;
run;

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;

PROC EXPORT ignoring Replace option

I have a SAS program which exports several tables to an Excel workbook. If the sheet specified in my PROC EXPORT does not exist in the Excel file, then a new sheet is created. However, if the sheet already exists, then nothing happens (although the SAS log tells me that the file was "successfully created"). It does not overwrite the existing data or create a new sheet.
I am using SAS 9.4 and exporting to Excel 2010.
proc export data=my.thing
outfile= "C:\Folder1\Folder2\myExcelFile.xlsx"
dbms=xlsx replace;
label;
sheet='thing';
run;
Your proc export statement looks okay. Perhaps the problem is when you're trying to overwrite the existing excel file. When you try to replace them, the existing excel file may not be in the expected format as the exporting excel file. Hence, there is trouble overwriting it. Look at the width of the variables in your existing excel and compare it with the new sheet "thing", maybe you can spot some inconsistencies.

How to create "standardized" Excel workbooks using SAS

I have a "wide" SAS data sets that must be exported into a new Excel workbook every week. I want to preserve the column widths and other Excel attributes every week, but I'm having problems getting it to work. Here's what I'm attempting.
I used PROC EXPORT to create a new workbook (using sheet="New_TACs").
I manually adjusted the column widths and other sheet attributes
(like "filters", column widths, wrap, alignment, and "freeze panes").
I deleted all the data rows (leaving the first row with the column
names) and saved it as a new workbook named "template.xlsx".
Using a SAS system call, I copy "template.xlsx" to "this_week.xlsx".
I use PROC EXPORT again to try and update the new workbook, but I
get warnings. The result contains a sheet named "New_TACS1".
Here is the SAS log:
720 proc export data=new_tacs
721 outfile="\\server-path\this_week.xlsx"
722 replace;
723 sheet='New_TACs';
724 run;
WARNING: The target file may contain unmatched range name and sheet name.
WARNING: The target file may contain unmatched range name and sheet name.
WARNING: File _IMEX_.New_TACs.DATA does not exist.
WARNING: Table _IMEX_."New_TACs" has not been dropped.
NOTE: "New_TACs" range/sheet was successfully created.
NOTE: PROCEDURE EXPORT used (Total process time):
real time 23.88 seconds
cpu time 1.80 seconds
I'm at a loss as to what to do and would appreciate any ideas or suggestions.
I think the issue is that with zero rows, SAS isn't properly dealing with the data. I can't get PROC EXPORT to work at all, but with a single dummy row I can at least get it to behave with libname and PROC APPEND. I wouldn't be surprise if the filters are in part responsible for this.
After creating the blank excel file with the SASHELP.CLASS columns, adding a filter, adding one row of dummy data, and saving/closing, I do: (SCANTEXT=NO is mandatory here for update access)
libname newtac "c:\temp\test.xlsx" scantext=no getnames=yes;
proc append base=newtac.'New_TACs$_xlnm#_FilterDatabase'n data=sashelp.class force;
run;
libname newtac clear;
That gets close, at least. I'm getting some blank rows for some reason, perhaps due to other things I did in looking at this.
Your best solution may well be to wait for 9.4 TS1M0 and ODS EXCEL, which will let you do all these things from SAS directly; or to use DDE.
I would recommend checking out SaviCells. http://www.sascommunity.org/wiki/SaviCells. It provides much better SAS to Excel functionality, including creating a template with all your Excel formatting and using that with new data.
Use DDE in SAS to achieve this.
You can create your excel template the way you want it to appear.
Using DDE you would then:
Open Excel
Open the excel file you want to use as the template
Populate it with the updated data
Save the file as a new filename
It's a bit of an antiquated technology but it gets the job done.
Googling for SAS and DDE will find you plenty of code exmaples and tutorials.