Hi all,
I am trying to conditionally rename variables if they are present or not in a dataset. Consider the example:
I read in the dataset, which sometimes there could be a case where the variable "Count2" exists, but I would like to test for its existence. If "Count2" not there I still get the warning:
"WARNING: The variable count2 in the DROP, KEEP, or RENAME list has never been referenced." I would like to get rid of this warning. What am I missing here? Your help would be greatly appreciated. Thanks.
data test;
input fruit $ count1;
datalines;
apple 12
banana 4
coconut 5
date 7
eggs 9
fig 5
;
data test2;
set test;
dsid=open('test');
check=varnum(dsid,'count2');
if check=0 then do;
rename count1 = amount1;
put 'Variable does not exist';
end;
else do;
rename count1 = amount1 count2 = amount2;
put 'Variable is located in column ' check +(-1) '.';
end;
run;
If you want to use your existing code then you will have to introduce some macro logic to get the desired results. Try this to see if that works
%macro check(dsname);
%let dsid=%sysfunc(open(&dsname));
%let countnum=%sysfunc(varnum(&dsid,count2));
%let rc=%sysfunc(close(&dsid));
%if &countnum=0 %then %do;
data test2;
set &dsname;
rename count1 = amount1;
%put 'Variable does not exist';
%end;
%else %do;
data test2;
set &dsname;
rename count1 = amount1;
rename count2 = amount2;
%put "Variable is located in column &countnum";
%end;
run;
%mend check;
%check(test)
Me personally, I would do:
data _null_;
set sashelp.vcolumn (where=(libname="WORK" and memname="TEST" and name="COUNT2"));
call execute('data test 2; (rename=(count2 = amount2)); run;');
run;
This utilizes the sashelp tables, and it will only generate the datastep if that variable is present (i.e. otherwise there would be no obs, and hence no loop).
You are getting message because you don't have count2 variable in test dataset.
That's correct stat@sas. That's by design, I know sometimes I will have 1 or 2 of the count variables. Count1 is guaranteed, Count2 could be in the dataset or not. I would like to test for that. One would think that since the there is a IF-THEN-ELSE statement on the "Check" variable, that that would work and not give me the sas Warning message.
If you want to use your existing code then you will have to introduce some macro logic to get the desired results. Try this to see if that works
%macro check(dsname);
%let dsid=%sysfunc(open(&dsname));
%let countnum=%sysfunc(varnum(&dsid,count2));
%let rc=%sysfunc(close(&dsid));
%if &countnum=0 %then %do;
data test2;
set &dsname;
rename count1 = amount1;
%put 'Variable does not exist';
%end;
%else %do;
data test2;
set &dsname;
rename count1 = amount1;
rename count2 = amount2;
%put "Variable is located in column &countnum";
%end;
run;
%mend check;
%check(test)
Awesome, stat@sas! That's what I needed.
Thanks you guys for all your input!
Rename statement is "Declarative" , meaning you can't execute it conditionally, unless you cheat it using Macro.
Haikuo
Add the system options will suppress these WARN message.
options dkricond= nowarn dkrocond=nowarn;
Xia Keshan
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!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.