Hi,
I was wondering if I could add log message to a dataset
I'am using the macro VarExists(ds,var) as an example:
%macro VarExist(ds,var);
%local rc dsid result;
%let dsid = %sysfunc(open(&ds));
%if %sysfunc(varnum(&dsid, &var)) > 0 %then %do;
%let result = 1;
%put Note: Var &var exists in %ds;
%end;
%else %do;
%let result = 0;
%put Note: Var &var not exists in &ds;
%end;
%let rc = %sysfunc(close(&dsid));
&result
%mend VarExists;
%varexist(sashelpclass, aaa);
I have little knowledge in SAS, but what I want (I will write a pseudo code below) is
if result = 1:
do nothing
if result = 0:
data want;
input message $;
datalines;
Note: Var &var not exists in &ds \* here &var and &ds will be the macro input*\
;
so there will be a final dataset that looks like this:
message
var aaa not exists in sashelp.class
Is there a way to do this? Thanks!
If you want a data set with a single record and variable it is likely easier to just assign a value to the variable instead of using data lines and you can't actually use macro variables in data lines.
data want; message = "Note: Var &var. does not exist in &ds."; run;
If you want to ADD this, as a different record to an existing data set then 1) make a temporary data set as above and 2) append the temporary set to the final data set. Your requirement isn't quite clear on the desired output.
The data step would go in the same place as the %let result =0; in the example code to create that WANT data set.
But you might describe what problem you are actually attempting to solve has this seems to be pretty convoluted approach to getting such a message into a data set.
If you are searching for a bunch of variables in multiple sets there are much more efficient ways to do this, especially if you have the data set names (and libraries involved) and variable names in a data set.
I'm trying to output all error messages in one big dataset... For this specific task I'm trying to find if certain variable exists. if it doesn't, output the error message to a data set. There are also other tasks like this too, And I'm just trying to create one report that contains all the errors.
I don't think proc compare will work? because you are comparing variables in different datasets, but I'm trying to do here is find if a variable exists in one data set.
If you want a data set with a single record and variable it is likely easier to just assign a value to the variable instead of using data lines and you can't actually use macro variables in data lines.
data want; message = "Note: Var &var. does not exist in &ds."; run;
If you want to ADD this, as a different record to an existing data set then 1) make a temporary data set as above and 2) append the temporary set to the final data set. Your requirement isn't quite clear on the desired output.
The data step would go in the same place as the %let result =0; in the example code to create that WANT data set.
But you might describe what problem you are actually attempting to solve has this seems to be pretty convoluted approach to getting such a message into a data set.
If you are searching for a bunch of variables in multiple sets there are much more efficient ways to do this, especially if you have the data set names (and libraries involved) and variable names in a data set.
%macro varexist(ds,var);
%local rc dsid result;
%let dsid = %sysfunc(open(&ds));
%if %sysfunc(varnum(&dsid,&var))>0 %then %do;
%let result = 1;
%end;
%else %do;
%let result = 0;
%end;
%if &result = 0 %then %do;
data error_data;
message = "Note: Var &var. does not exist in &ds.";
run;
%end;
%mend;
I tried this and get :statement is not valid or it is used out of proper order
@laiguanyu001 wrote:
%macro varexist(ds,var); %local rc dsid result; %let dsid = %sysfunc(open(&ds)); %if %sysfunc(varnum(&dsid,&var))>0 %then %do; %let result = 1; %end; %else %do; %let result = 0; %end; %if &result = 0 %then %do; data error_data; message = "Note: Var &var. does not exist in &ds."; run; %end; %mend;
I tried this and get :statement is not valid or it is used out of proper order
Copy from the LOG AFTER using OPTIONS MPRINT SYMBOLGEN with the 1) code defining the macro, 2) the call you made to the macro.
Worked for me:
74 %macro varexist(ds,var); 75 %local rc dsid result; 76 %let dsid = %sysfunc(open(&ds)); 77 %if %sysfunc(varnum(&dsid,&var))>0 %then %do; 78 %let result = 1; 79 %end; 80 %else %do; 81 %let result = 0; 82 %end; 83 84 %if &result = 0 %then %do; 85 data error_data; 86 message = "Note: Var &var. does not exist in &ds."; 87 run; 88 %end; 89 90 %mend; NOTE: The macro VAREXIST completed compilation without errors. 39 instructions 848 bytes. 91 92 %varexist(ds=sashelp.class, var=pdq); NOTE: The data set USER.ERROR_DATA has 1 observations and 1 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.01 seconds
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.