Bulkload w/Proc Append - sas

Relatively new to SAS and looking to bulkload data - pipe delimited .dlm - into a MySQL database. I've got the basics nailed down, I believe anyway, but I'm stumped on how to tell SAS the beginning and endings of a data row. Current syntax...
proc append base=abc.metrics (bulkload=yes
bl_delimter='|'
bl_options='errors=0'
bl_delete_datafile=no)
data=abc_metrics_&jid;
run;
Any thoughts, guidance, and comments would be appreciated.

I don't think you can directly do this for MySQL:
http://support.sas.com/documentation/cdl/en/acreldb/63647/HTML/default/viewer.htm#a002938849.htm
http://support.sas.com/documentation/cdl/en/acreldb/63647/HTML/default/viewer.htm#a001752476.htm
However, you can write out the file yourself. Change sashelp.class to your dataset, file "\server\yourfile.dlm", and if you need | after the last record prior to ~, remove that +(-1).
data _null_;
set sashelp.class;
file "c:\temp\test.dat" dlm='|' lrecl=32767;
put "!" #;
put name $ age height weight #;
put +(-1) "~";
run;
Then you can call MYSQL's bulk loading facility (whatever that is) as you normally would; you may be able to use a passthrough connection, ie,
proc sql;
connect to odbc (connection-information);
execute ("load file.dlm into table") by odbc;
quit;
(obviously bad sql syntax there, but that's the concept - anything in that " " is executed directly on the MySQL box as MySQL code as if you were in the DB's interface directly).
Or, if MySQL has something like Oracle's SQL*Loader, you can call that from the command line assuming you have access to it, using the 'x' command.

As far as I know, you cannot directly "bulk load" from SAS to MySQL, See this note in the SAS Access manual.
So, your best bet is to create a delimited text file (perhaps with PROC EXPORT) and use a mysql utility. Here is a previous SO question with various solutions, particularly the one about the load data local infile command.

Related

Is it possible to read RAW data type in SAS?

I am working with SAS and I am using data from an Oracle database via an ODBC connection. There are some fields I require from this database that have data_type = RAW in the Oracle SQL Developer environment.
SAS is reading these in incorrectly and is returning every field as 2A2A2A2A2A2A2A2A2A2A2A2A2A2A2A2A2A2A2A2A with Type = Character and Format and Informat = $HEX40.
One thing I tried to do is read it in as a character variable instead, using character formats and informats using the following code, where mylib is the library connected to an Oracle database.
data want;
set mylib.have (obs= 10000);
format raw_data_var char40.;
informat raw_data_var char40.;
run;
This changed the formats to character but it then converted the cells to ********************
I also tried find some SAS documentation on reading binary data, https://documentation.sas.com/?docsetId=lrcon&docsetTarget=p1vu9u7w1ieua7n17973igt2cq3c.htm&docsetVersion=9.4&locale=en
but unfortunately, I could not find something useful to help.
Can someone point me in the right direction to read in a raw data type using a data step or proc sql?
Thank you
You could use Proc SQL with a pass though query that utilizes the Oracle function RAWTOHEX
proc sql;
connect using mylib;
create table want as
select
a,b,c,input(rawhexed,$HEX32000.) as raw16kchars
from
connection to mylib
(
select a,b,c,rawtohex(myraw) as rawhexed
from have /* oracle side reference */
)
;
quit;

Read a sas7bdat file in SAS Studio

I've scoured the internet but cannot seem to figure this out. My question is, if I have a sas7bdat file, how can I read a sas7bdat file in SAS studio so that I can work with it.
I've tried:
libname test 'C:\Users\name\Downloads\test.sas7bdat';
which gives me the error that library test does not exist and if I try the following, I know that I need an INPUT which I don't know of unless I can see into the file.
DATA test;
INFILE 'C:\Users\lees162\Downloads\test.sas7bdat';
RUN;
Is there something I'm missing?
Libref's that you create via the LIBNAME statement point to directories, not individual files.
libname test 'C:\Users\name\Downloads\';
INFILE is for reading raw data files. To reference an existing SAS dataset you use a SET statement (or MERGE,MODIFY,UPDATE statement).
set test.test ;
Note that you can skip defining a libref and just use the quoted physical name in the SET statement.
DATA test;
set 'C:\Users\lees162\Downloads\test.sas7bdat';
RUN;
Of course to use C:\ in the paths this is assuming that you are using SAS/Studio to point to full SAS running on your PC. If you are using SAS University Edition then it is running in a virtual machine and you will need to put the SAS dataset into a folder that is mapped to the virtual machine and then reference it in the SAS code with the name that the virtual machine uses for the directory.
So something like:
DATA test;
set '/folders/myfolders/test.sas7bdat';
RUN;
Libname is just pointing the location and once you have done that you can use that libname followed period and dataset in your set statement
libname test "C:\Users\name\Downloads";
DATA test;
set test.asl;
RUN;
One possible reason could be that you are using the SAS University edition (It doesn't support variable library address).
From one of the SAS community Q/A:
"When you are using the SAS University Edition, any libraries that you create must be assigned to a shared folder. You access your shared folder with this pathname: /folders/myfolders/. Always use '/' in the directory path, even in Windows operating environments"
After setting the directory address, proceed as instructed by Tom above in one of the answers.
Suppose you have the sas dataset at location. C:\Users\name\Downloads\test.sas7bdat
libname download 'C:\Users\name\Downloads';
proc sql;
select * from downloads.test;
run;
you can read your dataset like a table using the proc sql, in case you want to query the dataset, but if you want to modify the existing dataset then you can use the data setp as mentioned by #krian.

DM command to open last dataset in rwork library

dm "vt &syslast";
The above command opens the last created dataset in work library.
How do I make it work for RWORK library?
dm "vt &syslast." will open the most recently created table, regardless of what location it is stored in. So if the most recently created table is in RWORK, that should not be a problem.
What might be a problem is the definition of &syslast, however. If you are doing something like:
rsubmit;
data class;
set sashelp.class;
run;
endrsubmit;
dm "vt &syslast.";
That won't work - &syslast is defined on the remote computer, not on your local computer. You either need to use %sysrput to put the macro variable onto your local machine, or know the name of the dataset you want to open.
rsubmit;
data class;
set sashelp.class;
run;
%sysrput rdata=&syslast;
endrsubmit;
dm "vt &rdata";
Or something along those lines. I think you'd probably have to translate the libname - &rdata would contain WORK.CLASS here, and you'd have to add an R - but it might be as simple as:
dm "vt R&rdata";
since you just want to prepend an R.
You could also directly specify the table, dm "vt rwork.tablename", and open any arbitrary table.
DM commands only work in SAS Display Manager environment (often called "Base SAS") and will not work in Enterprise Guide or SAS Studio. Both EG and Studio automatically open tables created during the current submission, by default, though the option to do so can be turned off.

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.

Import data from European Social Survey

I need to import data from European Social Survey databank to SAS.
I'm not very good at using SAS so I just naively tried importing the text file one gets but it stores it all in one variable.
Can someone maybe help me with what to do? Since there doesn't seem to be a guide on their webpage I reckon it has to be pretty easy.
It's free to register (and takes 5 secs) and I need all possible data for Denmark.
Edit: When downloading what they call a SAS file, what i get is a huge proc format and the same text file as one gets by choosing text.
The data in the text file isn't comma separated and the first row does not contain variable names.
Download it in SAS format. Save the text file in a location you can remember, and open the SAS file. It's not just one big proc format; it's a big proc format followed by a datastep with input code. It was probably created by SPSS (it fits the pattern of an SPSS saved .sas file anyhow). Look for:
DATA OUT.ESS1_4e01_0_F1;
Or something like that (that's what it is when I downloaded it). It's probably about 3/4 of the way down the page. You just need to change the code:
INFILE 'ESS1_4e01_0_F1.txt';
or similar, to be the directory you placed the text file in. Create a LIBNAME for OUT that goes to wherever you want to permanently save this, and do that at the start of the .sas file, replacing the top 3 lines like so.
Originally:
LIBNAME LIBRARY '';
LIBNAME OUT '';
PROC FORMAT LIBRARY=LIBRARY ;
Change these to:
libname out "c:\mystuff\"; *but probably not c:\mystuff :);
options fmtsearch=(out);
proc format lib=out;
Then run the entire thing.
This is the best solution if you want the formatted values (value labels) and variable labels. If you don't care about that, then it might be easier to deal with the CSV like Bob shows.
But the website says yu can download SAS format, why don't you?
You need a delimiter if all goes into one column.
data temp;
length ...;
infile 'file.csv' dlm=',';
input ...;
run;
As Dirk says, the web site says you can download a SAS dataset directly. However, if there's some reason you don't want to do that, choose a comma separated file (CSV) and use PROC IMPORT. Here is an example:
proc import out=some_data
datafile='c:\path\somedata.csv'
dbms=csv replace;
getnames=yes;
run;
Of course, this assumes the first row contains column names.