Help using Base SAS procedures

First SAS program

Reply
New Contributor
Posts: 4

First SAS program

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,

PROC Star
Posts: 7,366

First SAS program

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.

New Contributor
Posts: 4

First SAS program

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

PROC Star
Posts: 7,366

First SAS program

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.

New Contributor
Posts: 4

First SAS 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;

PROC Star
Posts: 7,366

First SAS program

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;

Ask a Question
Discussion stats
  • 5 replies
  • 372 views
  • 0 likes
  • 2 in conversation