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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
stat_sas
Ammonite | Level 13

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

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

stat_sas
Ammonite | Level 13

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

avbraga
Calcite | Level 5

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.

stat_sas
Ammonite | Level 13

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)

avbraga
Calcite | Level 5

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

Thanks you guys for all your input!

Haikuo
Onyx | Level 15

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

Haikuo

 

Ksharp
Super User

Add the system options will suppress these WARN message.

options dkricond= nowarn  dkrocond=nowarn;

Xia Keshan

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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