Help using Base SAS procedures

Open Function with Variable for Data Set Name

Reply
New Contributor
Posts: 3

Open Function with Variable for Data Set Name

Hi,

I've been searching for a solution for this problem for the past couple of days with no luck.

I have a data set with a column called mail_file that has a file paths to other SAS data sets.  I want to open those data sets and check to see if certain variables exist.

This is the code I came up with:

data lists;

     set list_to_load;

     dsid=open(mail_file, 'I' , , 'F');

    

     if dsid^=0 then

     do;

          if varnum(dsid, 'PROMOCODE')=0 then

               column_exists=1;

     end;

run;

I always get a dsid of 0.

The paths the of the files look like this: S:\Marketing\For_Output\listloadtest.sas7bdat.

Is there any way I can use a variable in the open() function? Or is there another approach I can take to acomplish this task?

Thanks a alot.

PROC Star
Posts: 7,416

Open Function with Variable for Data Set Name

Your code appears to work if you leave off the extension.  e.g., try the following:

libname art "c:\art";

data art.test1;

  set sashelp.class;

  retain PROMOCODE (1);

run;

data art.test2;

  set sashelp.class;

run;

data art.test3;

  set sashelp.class;

  retain PROMOCODE (1);

run;

data list_to_load;

  informat mail_file $30.;

  input mail_file;

  cards;

c:\art\test1

c:\art\test2

c:\art\test3

;

data lists;

  set list_to_load;

  dsid=open(mail_file, 'I' , , 'F');

  if dsid^=0 then

  do;

       if varnum(dsid, 'PROMOCODE')=0 then

            column_exists=1;

  end;

run;

Super User
Super User
Posts: 6,701

Open Function with Variable for Data Set Name

Did you try quoting the filename?

   open(quote(trim(mail_file)),.....

Otherwise you will need to create a libref pointing the directory and then try to open the file using the traditional libname.member two level dataset name format.

fname=scan(mail_file,-1,'/\');

dir=substr(mail_file,1,length(mail_file)-length(fname));

member=scan(fname,1,'.');

libref='MYDATA';

rc=libname(libref,dir,,'access=readonly');

** test for error codes ?? ;

rc=open('MYDATA.'||member,.....)

...

New Contributor
Posts: 3

Open Function with Variable for Data Set Name

Thanks for both of your responses!

I definitely needed the trim() function and it looks like the mode I using was not working correctly.

When I use open(trim(mail_file), , , 'F',) everything works as it should.

Thanks again.

Ask a Question
Discussion stats
  • 3 replies
  • 123 views
  • 6 likes
  • 3 in conversation