11-30-2015 02:06 AM
hello am creating this macro to provide information on whether a list of variables exist within a dataset.
%let dsname= ;
%let dsid = %sysfunc(open(dsname));
%if %sysfunc(varnum(&dsid))> 0 %then %do;
%put NOTE: Variable x exists!;
%let rc = %sysfunc(close(&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.
11-30-2015 04:57 AM
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.
11-30-2015 05:21 AM - edited 11-30-2015 05:27 AM
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.
11-30-2015 05:48 AM
@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.
11-30-2015 06:22 AM
11-30-2015 06:42 AM
Well, a quick look into the online help on the VARNUM function reveals that it requires two arguments:
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.
11-30-2015 06:47 AM
11-30-2015 09:03 AM
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!;
%put variable does not exists!;
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!;
%put variable &varname does not exist!;
12-01-2015 02:49 AM