BookmarkSubscribeRSS Feed
petlove
Obsidian | Level 7

Hi all,

 

 

Can we write IF condition for all character variables of dataset without mentioning their names in sas steps?

 

 

Thank you.

 

 

7 REPLIES 7
Reeza
Super User

It depends.

 


@petlove wrote:

Hi all,

 

 

Can we write IF condition for all character variables of dataset without mentioning their names in sas steps?

 

 

Thank you.

 

 


 

ballardw
Super User

@petlove wrote:

Hi all,

 

 

Can we write IF condition for all character variables of dataset without mentioning their names in sas steps?

 

 

Thank you.

 

 


Provide a concrete example of what you want to accomplish. Depending on what the "if condition" is supposed to accomplish can make a big difference on what can be done.

 

If you want to do something with each variable at a time, probably yes. If you mean all variables at once then most likely no.

 

In between those two it really really depends on what you are trying to do as to the likelihood of practicality and complexity of code.

petlove
Obsidian | Level 7

Thank you for your response.

 

I am replacing missing values of character variables to '--' and numeric values to 0 by using below array:

 

array charv(*) _character_;
do _n_ =1 to dim(charv);
charv(_n_)=coalescec(charv(_n_), '--');
end;

 

array numv _numeric_;
do over numv ;
if numv =. then numv =0;
end;

 

When I run this code for any dataset and if dataset does not have character variable it shows below WARNING. In order to avoid, i was thinking to write IF condition.

 

WARNING: Defining an array with zero elements.

 

Thank you.

PGStats
Opal | Level 21

Do this instead

 

_dumx = 999;
_dumc = "X";
array charv(*) _character_;
do _n_ =1 to dim(charv);
    charv(_n_)=coalescec(charv(_n_), '--');
    end;

array numv _numeric_;
do _n_ = 1 to dim(numv) ;
    numv(_n_) = coalesce(numv(_n_), 0);
    end;
drop _dum: ;
PG
Tom
Super User Tom
Super User

To eliminate the warning you can force SAS to create new variables and then drop them. 

Pick names that you know your real data will never have.  

Here is example you can run that uses SASHELP.CLASS and drops all of the numeric or characfer variables on the way in.

data test_nochar;
  set sashelp.class(drop=_character_) ;
  array _1 _numeric_ _drop_numeric_;
  array _2 _character_ _drop_character_ ;
  drop _drop_numeric_ _drop_character_ ;
  do over _1; _1=coalesce(_1,0); end;
  do over _2; _2=coalescec(_2,'--'); end;
run;

data test_nonumb;
  set sashelp.class(drop=_numeric_) ;
  array _1 _numeric_ _drop_numeric_;
  array _2 _character_ _drop_character_ ;
  drop _drop_numeric_ _drop_character_ ;
  do over _1; _1=coalesce(_1,0); end;
  do over _2; _2=coalescec(_2,'--'); end;
run;
PGStats
Opal | Level 21

Sometimes, yes... For example,

 

if you want to check for empty strings

 

if cmiss(of _character_) > 2  then delete;

 

if you want to check for the presence of a word

 

if whichc("Error", of _character_) > 0 then delete;

 

if you want to check for the presence of special characters

 

if anycntrl(cats(of _character_)) > 0 then delete;

 

you get the idea...

 

 

PG

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!

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
  • 1446 views
  • 4 likes
  • 5 in conversation