Help using Base SAS procedures

Execute proc export with an if statement

Accepted Solution Solved
Reply
Contributor
Posts: 68
Accepted Solution

Execute proc export with an if statement

Hi all

I have the following code:

data a;

set b;

if variable x ne variable y;

run;

proc export data = a dbms= excel

outfile = path to safe file;

sheet = "test";

run;

Is there any option to run the proc export only if the number of observations is greater than 0?

This code is repeated comparing two variables each time and most of the time there are no observarions in the output.

thanks to all

Regards


Accepted Solutions
Solution
‎05-27-2014 04:37 AM
Super User
Super User
Posts: 7,997

Re: Execute proc export with an if statement

Posted in reply to SergioSanchez

Hi,

Not sure why you need all that macro code and file open close etc.  If you want to do a whole library of datasets where observations is greater than 0 then do:

data _null_; 

  set sashelp.vtable (where=(libname="your libname here" and nobs=0) keep=memname);

  call execute('proc export data=your_libname.'||strip(memname)||' dbms= excel

                                   outfile="s:\temp\rob\export_file_'||strip(memname)||'.xls;

                  sheet = "test";

                run;');

run;

This will select any dataset with > 0 observations in the libname and create export statements for each one.

View solution in original post


All Replies
Super User
Posts: 19,878

Re: Execute proc export with an if statement

Posted in reply to SergioSanchez

You could wrap it in a macro. If you're repeating this multiple times, it probably should be in a macro anyways.

data class;

set sashelp.class;

if age in (13, 14) then height=age;

run;

%macro export_conditionally(var1, var2);

data a;

set class;

if &var1 eq &var2;

run;

proc sql noprint;

select count(*)  into :count from a;

quit;

%if &count ne 0 %then %do;

    proc export data=a dbms=xls

    outfile="C:\_localdata\temp.xls";

    sheet="test";

    run;

%end;

%mend;

%export_conditionally(weight, height)

%export_conditionally(age, height)

Trusted Advisor
Posts: 1,231

Re: Execute proc export with an if statement

Posted in reply to SergioSanchez

Hi,

This may be helpful in your desired solution.

Thanks,

Naeem

%macro export (dsname);

%let dsid=%sysfunc(open(&dsname));

%let nobs=%sysfunc(attrn(&dsid,nobs));

%let rc=%sysfunc(close(&dsid));

title "Num Obs: &nobs";

%if &nobs>0 %then %do;

data a;

set &dsname;

if x ne y;

run;

proc export data = a dbms= excel

outfile = 'path to safe file';

sheet = "test";

run;

%end;

%mend export;

%export(b)

Super User
Posts: 19,878

Re: Execute proc export with an if statement

The open/close is probably a better way to get the count.

However, if you're comparing variables and looking for identical values you may want to look into proc compare.

Contributor
Posts: 68

Re: Execute proc export with an if statement

Thanks for your help Reeza

I look proc compare, it´s seems a better solution.

Solution
‎05-27-2014 04:37 AM
Super User
Super User
Posts: 7,997

Re: Execute proc export with an if statement

Posted in reply to SergioSanchez

Hi,

Not sure why you need all that macro code and file open close etc.  If you want to do a whole library of datasets where observations is greater than 0 then do:

data _null_; 

  set sashelp.vtable (where=(libname="your libname here" and nobs=0) keep=memname);

  call execute('proc export data=your_libname.'||strip(memname)||' dbms= excel

                                   outfile="s:\temp\rob\export_file_'||strip(memname)||'.xls;

                  sheet = "test";

                run;');

run;

This will select any dataset with > 0 observations in the libname and create export statements for each one.

Contributor
Posts: 68

Re: Execute proc export with an if statement

Thanks EW9.

it´s seems is an easy way to achieve the goal.

Edit;

Sorry for the incovenience but  I try to run the following code:

data _null_; 
set sashelp.vtable (where=(libname="difiere" and nobs=0) keep=memname);
call execute('proc export data=difiere.'||strip(memname)||' dbms= excel
              outfile="c:\dirtest\export_file_'||strip(memname)||'.xls;
              sheet = "test";
              run;');

run;

and I revieve this error in the log:

ERROR: Variable libname is not on file SASHELP.VTABLE.

I don´t understand anything, I can open the vtable dataset and see the variable.

Why do I get this message?

Thanks,

Contributor
Posts: 68

Re: Execute proc export with an if statement

Posted in reply to SergioSanchez

Done

Thanks to all for the help.

Regards.

Super User
Posts: 19,878

Re: Execute proc export with an if statement

Posted in reply to SergioSanchez

Please consider marking the question as answered for future users.

Contributor
Posts: 68

Re: Execute proc export with an if statement

Sorry

It´s already done.

Regards


🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 2349 views
  • 11 likes
  • 4 in conversation