How ?

Reply
Frequent Contributor
Posts: 86

How ?

Hi,

How to check this...Need a MACRO code

  • Dataset name is correct or not
  • How many OBS is there
  • How many verables is there
  • Location

Thanks,

Smiley Happy

Respected Advisor
Posts: 3,156

Re: How ?

Hi,

Since the question is a bit of vague, I will go ahead make some assumptions. First, this is for SAS data set, not external file. Second, when referring 'location', we are talking about the phsical, not the logical one, as you need to know the logical one (libname) to start with. So here it goes, hopefully it can get you started:

data have;

set sashelp.class;

run;

%macro info(lib=, dsn=);

data _null_;

a=exist("&dsn"); /*check existance of a SAS table*/

if a then rc1=open("&dsn");

var_number=attrn(rc1,'nvars'); /*the total number of variables*/

total_obs=attrn(rc1,'nobs'); /*the total number of obs*/

call symputx('_existance',a);

call symputx('_varnum',var_number);

call symputx('_nobs',total_obs);

run;

/*get the physical path*/

proc sql noprint;

select path into :_path

from dictionary.libnames

where libname="%upcase(&lib)";

quit;

%put    path: &_path;

%put    existance (0/N, 1/Y): &_existance;

%put    variable number: &_varnum;

%put    Total number of records: &_nobs;

%mend;

%info(lib=work,dsn=have);

Haikuo

Frequent Contributor
Posts: 86

Re: How ?

Thank you Haikuo

small change

  • If 'Rows >0' only it should run
  • It should include 'Proc Contents'

Super Contributor
Posts: 1,636

Re: How ?

I added the RED parts to HaiKuo's code.

data have;
set sashelp.class;
run;
 
%macro info(lib=, dsn=);
data _null_;
a=exist("&dsn"); /*check existance of a SAS table*/
if a then rc1=open("&dsn");
var_number=attrn(rc1,'nvars'); /*the total number of variables*/
total_obs=attrn(rc1,'nobs'); /*the total number of obs*/
call symputx('_existance',a);
call symputx('_varnum',var_number);
call symputx('_nobs',total_obs);
run;
%if &_nobs>0 %then %do;
  proc contents data=&lib..&dsn;run;%end;

 
/*get the physical path*/
proc sql noprint;
select path into :_path
from dictionary.libnames
where libname="%upcase(&lib)";
quit;
%put    path: &_path;
%put    existance (0/N, 1/Y): &_existance;
%put    variable number: &_varnum;
%put    Total number of records: &_nobs;
 
%mend;
 
%info(lib=work,dsn=have);

Respected Advisor
Posts: 3,156

Re: How ?

Why I smell some 'homework' Smiley Wink

If it is homework, then those I/O functions could be out of scope, and Proc contents comes into play. Here is another try using Proc Contents only, please note, it requires full name including library:

%macro info (dsn=);

ods listing close;

ods output attributes=want;

proc contents data=&dsn ;

run;

ods output enginehost=want1;

proc contents data=&dsn ;

run;

ods output close;

ods listing ;

data _null_;

set want;

if Label2='Observations' then call symputx('Obs',cValue2);

else if Label2='Variables' then call symputx('vars',cvalue2);

run;

data _null_;

set want1;

if Label1='Filename' then do;

   call symputx('path', cvalue1);

   stop;

end;

run;

%if &obs>0 %then %do;

%put    path: &path;

%put    variable number: &vars;

%put    Total number of records: &Obs;

%end;

%mend;

%info(dsn=sashelp.class);

Haikuo


Frequent Contributor
Posts: 86

Re: How ?

One more sorry

  • How to Check Data set variables is same or not
  • Dataset is existance or not

Super User
Posts: 11,343

Re: How ?

Your call to proc contents will report on variables that appear in only one of the datasets. Or do you want something else.

From the online help to find the existence of a dataset (one way at least)

%let dsname=sasuser.houses;

%if %sysfunc(exist(&name)) = 1 %then %do;

   <whatever you want to do when it exists, actually the =1 isn't required but the %sysfunc will return 1 or 0>

%end;

Respected Advisor
Posts: 3,799

Re: How ?

PROC CONTENTS

Ask a Question
Discussion stats
  • 7 replies
  • 273 views
  • 1 like
  • 5 in conversation