Hi All,
I'm writing my first SAS program and I need some guidance on something very simple:
I need to read a dataset from an excel file with multiple sheets (but I need to read one sheet). Here's the code I came with that doesn't work sofar. I needed to print the first 20 observations
* Read data from external file into SAS data set;
DATA Orders;
INFILE 'C:\MySAS\Orders.xls';
sheet = "test";
INPUT OrdNum CustName CustAdd CustCd Item Price;
Run;
PROC PRINT DATA = Orders;
sheet = "test";
TITLE 'MyOrders';
Run;
Any help is greatly appreciated.
Thanks,
You can't use the input method you've shown for importing a spreadsheet. You can either use proc import, assigning the spreadsheet to a libname, or what is called DDE.
You can accomplish the first one simply by clicking on file import, from the menu bar. If you want to see the code that is actually submitted, during the processing one of the questions the system will ask you is whether you want to save the program that was created.
Thanks for the response. I have tried to import the file, but I keep getting errors that the class is not registered and error in the LIBNAME statement.
Thanks
The first two methods I suggested require the license of a product called SAS/Access for pcfiles. If you don't have that, then your alternatives are either to used DDE (which I wouldn't recommend for someone who is just learning SAS), an already developed macro that uses DDE in the background (which I also wouldn't recommend for someone who is just learning SAS),
or saving the spreadsheet (from within Excel) to a comma delimited file (csv) and then using code very similar to that which you originally were trying to use.
Again, in the last case, I'd use proc import and have it save the resulting program and then reviewing the resulting program.
I managed to get it to work using this code:
PROC IMPORT OUT= WORK.Orders DATAFILE="C:\MySAS\Orders.xls" DBMS=xls REPLACE;
SHEET="testing";
GETNAMES=YES;
PROC Contents DATA = Work.Orders;
Proc Print DATA = Work.Orders (obs=10);
Title "MyOrders";
Run;
But I have this question:
I need to create a new variable "NewPrice" to convert the "Price" into a binary variable and assign the value "1" if the "Price" >= 20, otherwise assign "0"
Here's my code that didn't work:
INPUT "NewPrice $";
If 'Price' < 20 then 'NewPrice' = 0;
else 'NewPrice' = 1;
Run;
You created a SAS file called orders. All you have to do, now, is modify that file:
data orders;
set orders;
if Price < 20 then NewPrice = 0;
else NewPrice = 1;
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.