BookmarkSubscribeRSS Feed
Sasanytics
Calcite | Level 5

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,

5 REPLIES 5
art297
Opal | Level 21

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.

Sasanytics
Calcite | Level 5

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

art297
Opal | Level 21

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.

Sasanytics
Calcite | Level 5

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;

art297
Opal | Level 21

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2126 views
  • 0 likes
  • 2 in conversation