BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
laiguanyu001
Fluorite | Level 6

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

8 REPLIES 8
Reeza
Super User
There is a way, but honestly, its not a good idea.

What are you trying to do overall? PROC COMPARE will compare data sets to ensure variables are the same and formats/lengths and print a report for example.

laiguanyu001
Fluorite | Level 6

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.

Reeza
Super User
Then I would make a specific set of codes that does each task and creates an error message within that proc that appends to a master error table. This is a method I've used before but it's pretty manual. If you're using SAS EG or Studio, all the errors or notes are easily navigable so this seems redundant as well.
ballardw
Super User

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.

laiguanyu001
Fluorite | Level 6
%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

Reeza
Super User
Post the log/actual error. Where the error occurs is noted in the log and makes it much easier to debug. You should also be using MPRINT/SYMBOLGEN to help you debug.
ballardw
Super User

@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







Reeza
Super User
Here's an example I wrote a while back, to compare two data sets and generate a short data set/report on the variables.

https://gist.github.com/statgeek/e0b98c4627aa31a567e5

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 8 replies
  • 1083 views
  • 0 likes
  • 3 in conversation