debug code

Reply
Regular Contributor
Posts: 190

debug code

hello am creating this macro to provide information on whether a list of variables exist within a dataset.


%let dsname= ;
%macro varexist(dsid=);
%let dsid = %sysfunc(open(dsname));
%if %sysfunc(varnum(&dsid))> 0 %then %do;
%put NOTE: Variable x exists!;
end;
%else %do;
%let rc = %sysfunc(close(&dsid));
end;
%mend varexist(dsid);

plz see if my code is correct or suggest me any changes or provide ur code for 

a generic macro to provide information on whether a list of variables exist within a dataset.

Super User
Super User
Posts: 7,392

Re: debug code

Or you could just use/refer to SASHELP.VTABLE/VCOLUMN, or in SQL, DICTIONARY.COLUMNS/TABLES, is automatic, requires no code, and anyone familiar with SAS should be aware of it - so doesn't have to learn your particular macros.

Regular Contributor
Posts: 190

Re: debug code

i know ur solution but i want to create my own personal macro so can u help me with d code
Respected Advisor
Posts: 3,887

Re: debug code

[ Edited ]

If you don't want to go for the common solution which is easy to read and maintain but want to create your own macros for educational purposes then I'd suggest you do the work on your own. That's how you learn.

 

If you have a specific problem to solve while developing your own macros then I suggest you post the code, the log, explain the explict problem and what you need. I don't feel it's sufficient to just post code and then expect "solve everything for me". At least I for my part won't spend time for such requests.

 

"plz see if my code is correct "

Show us some of your testing. What have you done? Which one were your test cases and how have you executed them? May be then someone can point you to a case you've missed where your code doesn't work.

 

 

Trusted Advisor
Posts: 1,115

Re: debug code

@RTelang, if you set out to write a standard SAS macro, you should start with writing down specifications.


I think it would not be specific enough if you wrote the macro should "provide information ..." because there are quite different ways to do so: The macro could set a macro variable to 1 if the list of variables exists, it could return the value 1 (function-style macro) or it could write a message to the log, to name just three examples.


Another important specification would answer the question: How will the "list of variables" be passed to the macro?

 

Once you have finished a first preliminary macro version (with possibly only limited functionality), your code must be syntactically correct. Otherwise you cannot test it.


If you submit the code you have provided, you will see from the error and warning messages in the log that SAS's macro processor has some issues with it.


After the corrections -- adding the missing % signs before "end" and removing "(dsid)" from the %MEND statement -- you could check if your macro meets the specifications (had you written them).


Currently, I'm afraid, this would not be the case, because you forgot to pass the "list of variables" to the macro.

 

Regular Contributor
Posts: 190

Re: debug code

56 %let dsname= ;
57 %macro varexist();
58 %let dsid =%sysfunc(open(dsname));
59 %if %sysfunc(varnum(&dsid),x)> 0 %then %do;
60 %put Variable x exists!;
61 %end;
62 %else %do;
63 %put variable does not exists!;
64 %end;
65 %mend varexist;
66 %varexist;
ERROR: The function VARNUM referenced by the %SYSFUNC or %QSYSFUNC macro function has too few arguments.
variable does not exists!
yes i have done necessary changes i posted the old code this is d new one with the log error i cannot understand it can u explain it to me?
Trusted Advisor
Posts: 1,115

Re: debug code

Well, a quick look into the online help on the VARNUM function reveals that it requires two arguments:

VARNUM(data-set-id, variable-name)

 

You call it with only one argument: varnum(&dsid).

 

Moreover, macro variable DSID most probably contains the value 0. (You can check this outside the macro.) But 0 is not a valid first argument for VARNUM.

Regular Contributor
Posts: 190

Re: debug code

56 %let dsname= sashelp.class;
57 %let varname=name;
58 %macro varexist();
59 %let dsid =%sysfunc(open(&dsname));
60 %if %sysfunc(varnum(&dsid,&varname))> 0 %then %do;
61 %put Variable x exists!;
62 %end;
63 %else %do;
64 %put variable does not exists!;
65 %end;
66 %mend varexist;
67 %varexist;
Variable x exists!
did it got the answer!!
Super User
Posts: 5,079

Re: debug code

For you to determine whether you got the right answer,  make a couple of small changes.  Current code:


 %if %sysfunc(varnum(&dsid,&varname))> 0 %then %do;
 %put Variable x exists!;
 %end;
 %else %do;
 %put variable does not exists!;
 %end;

 

The %PUT messages should tell you whether you got it right or not.  To make that happen, change this code to:

 

%if %sysfunc(varnum(&dsid,&varname))> 0 %then 
 %put Variable &varname exists!;
%else 
 %put variable &varname does not exist!;

 

Good luck.

Regular Contributor
Posts: 190

Re: debug code

thanks mate i changed my code

%macro varexist (dsname=, varlist=, outputflag=);
%local i nextword;
%let dsid =%sysfunc(open(&dsname));
%let &outputflag=1;
%do i=1 %to %sysfunc(countw(&varlist));

%let nextword = %scan(&varlist, &i);

%if %sysfunc(varnum(&dsid,&nextword))> 0 %then %do;

%put Variable &nextword exists!;
%end;
%else %do;
%let &outputflag=0;
%put variable &nextword does not exist;
%end;
%end;
%mend varexist;
Regular Contributor
Posts: 190

Re: debug code

i created new code it works fine thanks for d help As
Ask a Question
Discussion stats
  • 10 replies
  • 518 views
  • 0 likes
  • 5 in conversation