Help using Base SAS procedures

SAS Studio On Demand for Academics--Infile Statement

Reply
Contributor
Posts: 35

SAS Studio On Demand for Academics--Infile Statement

I can't figure out a way to use an infile statement that will actually work. I put in the whole path and it doesn't work. I've tried assigning a libref and then the name of the file  eg infile mylib.myfile.

I tried quotes everything. can it not read raw data ?

Super User
Posts: 11,343

Re: SAS Studio On Demand for Academics--Infile Statement

You should be getting error messages. Please post some of them.

Super User
Posts: 19,878

Re: SAS Studio On Demand for Academics--Infile Statement

Yes, but you usually need to upload the data to the server, it can't access data on your desktop AFAIK. You usually need to be a professor to accomplish this as well.

There are quite a few posts on here regarding this issue. 

Contributor
Posts: 35

Re: SAS Studio On Demand for Academics--Infile Statement

ERROR: Physical file does not exist, /pbr/biconfig/940/Lev1/SASApp/country.dat.

looks like it's looking elsewhere for it despite the libname was assigned succefully

I can't find anything on this topic w google incidentally

Super User
Posts: 19,878

Re: SAS Studio On Demand for Academics--Infile Statement

Super User
Super User
Posts: 7,083

Re: SAS Studio On Demand for Academics--Infile Statement

You do not read a text file using a libref.  You can use a fileref.  Or you can just use a quoted physical path.

If your LIBNAME statement executed without error then you could try using the same path to reference the file.

Make sure to use a fully qualified path, because otherwise SAS will look for the file starting the current working directory which is usually not your path but part of the SAS installation.

%let path=/home/....... ;

libname out "&path" ;

data out.new ;

infile "&path/myfile.csv" dsd firstobs=2 ;

input var1-var5;

run;

SAS Super FREQ
Posts: 8,869

Re: SAS Studio On Demand for Academics--Infile Statement

Hi:

  These 2 previous postings specifically show reading COUNTRY.DAT with an INFILE statement:

INFILE statement for PRG1 on Univ Edition:

  https://communities.sas.com/message/222742#222742 (example country.dat)

  https://communities.sas.com/message/224467#224467 (example country.dat)

  And, I believe that COUNTRY.DAT is used in the Programming 1 course. In the e-learning course, the instructions in the Course Overview section have you explicitly set a macro variable called PATH like this:

%let path = /folders/myfolders/ecprg193;

which means that the correct INFILE statement for COUNTRY.DAT would be one of these (assuming that you followed the instructions and made an ECPRG193 directory):

INFILE '/folders/myfolders/ecprg193/country.dat' dlm='!';

INFILE "&path/country.dat" dlm='!';

  If you are still using your course notes from Programming 1 and Programming 2, as you indicated in this posting, https://communities.sas.com/message/257003#257003 then you would follow the instructions in the first demo in Chapter 1.

cynthia

Contributor
Posts: 35

Re: SAS Studio On Demand for Academics--Infile Statement

Posted in reply to Cynthia_sas

I am really confused.

1. I'm using On Demand not the virtual machine.

2. %Let is not something I am familiar with

3. I'm using Country.dat for my own reasons and not necessarily following the course notes.

4. When I use a libname statement and a set statement to read a SAS file; it works. Why wouldn't the same libname statement with an infile statement to read raw data (in the same folder as the SAS data set) not work also?

And please reply with just an infile statement that contains the syntax I should be using. Thanks.

SAS Super FREQ
Posts: 8,869

Re: SAS Studio On Demand for Academics--Infile Statement


Hi:

  If you are using SAS OnDemand for Academics, then instead of /folders/myfolders you would use your /home directory, as shown in your SAS OnDemand Control Center:

/home/<your_username>/ecprg193/country.dat so if your username is nyioves, for example and if you did make a folder called ecprg193 for the data then the infile would be:

infile '/home/nyioves/ecprg193/country.dat' dlm='!'; OR, if you make a different file for the course programs, then the infile would be:
    

infile '/home/nyioves/prg1/country.dat' dlm='!';
      

The LIBNAME points to SAS datasets--so the LIBNAME statement will NOT contain any reference to a specific file name and file extension. On the other hand, the INFILE statement points to raw data files, so the INFILE statement should have a reference to a specific file. So if you have a LIBNAME statement like this:

libname orion '/home/nyioves/ecprg193';
     

then your INFILE statement should be the SAME as the high level path with the raw data file name specified at the end.

infile '/home/nyioves/ecprg193/country.dat' dlm='!';

You said that you have a working LIBNAME statement. Assuming that country.dat is in the same location as the SAS datasets, then you should be able to use the path from your LIBNAME statement. But without knowing 1) your username and 2) the name of the folder you have, it is impossible to just give you just the syntax that you should be using.

cynthia

Contributor
Posts: 35

Re: SAS Studio On Demand for Academics--Infile Statement

Posted in reply to Cynthia_sas

Tom got it but not necessarily I think for the reasons he montioned. You need a fileref since course has 2 Country files -- .dat and a SAS.

Anyway you need a fileref....

filename nikfile '/home/nyioves/Nikfold/country.dat';

data myfil;

infile nikfile dlm '!';

input var1 $ var2 $;

run;

Super User
Posts: 19,878

Re: SAS Studio On Demand for Academics--Infile Statement

You do read text files and SAS files differently in SAS. You use a libname to reference SAS (.sas7bdat) datasets and filerefs to reference text files.

You can also reference a text file directly in the data step. If your question is answered please mark it answered.

ie.

data myfil;

infile '/home/nyioves/Nikfold/country.dat' dlm '!';

input var1 $ var2 $;

run;

SAS Super FREQ
Posts: 8,869

Re: SAS Studio On Demand for Academics--Infile Statement

Hi, there are several different ways to point to external files:


1) using a FILEREF and an INFILE statement:
filename wombat '/home/<username>/subdir/filename.ext';

 
data work.new;
  infile wombat dlm=',';
... more code...;
run;

    

2) direct reference:
data work.new;
  infile '/home/<username>/subdir/filename.ext' dlm=',';
... more code...;
run;


3) combined or "member" reference:
filename wombat '/home/<username>/subdir';

 

data work.new;
  infile wombat(filename.ext) dlm=',';
... more code...;
run;

The key is understanding the path name and that the filename/extension are needed for all 3 methods: #1 and #2, the filename and extension are needed in either the FILENAME or INFILE statement and for method #3, the FILENAME statement does NOT have the file name and extension, but the INFILE statement does have the name and extension.

Cynthia 

Ask a Question
Discussion stats
  • 11 replies
  • 1787 views
  • 4 likes
  • 5 in conversation