DATA Step, Macro, Functions and more

conditionally renaming variables

Accepted Solution Solved
Reply
Contributor
Posts: 45
Accepted Solution

conditionally renaming variables

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;


Accepted Solutions
Solution
‎06-24-2014 02:54 PM
Trusted Advisor
Posts: 1,204

Re: conditionally renaming variables

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)

View solution in original post


All Replies
Super User
Super User
Posts: 7,401

Re: conditionally renaming variables

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).

Trusted Advisor
Posts: 1,204

Re: conditionally renaming variables

You are getting message because you don't have count2 variable in test dataset.

Contributor
Posts: 45

Re: conditionally renaming variables

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.

Solution
‎06-24-2014 02:54 PM
Trusted Advisor
Posts: 1,204

Re: conditionally renaming variables

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)

Contributor
Posts: 45

Re: conditionally renaming variables

Awesome, stat@sas! That's what I needed.

Thanks you guys for all your input!

Respected Advisor
Posts: 3,124

Re: conditionally renaming variables

Rename statement is "Declarative" , meaning you can't execute it conditionally, unless you cheat it using Macro.

Haikuo

 

Super User
Posts: 9,681

Re: conditionally renaming variables

Add the system options will suppress these WARN message.

options dkricond= nowarn  dkrocond=nowarn;

Xia Keshan

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 1515 views
  • 0 likes
  • 5 in conversation