BookmarkSubscribeRSS Feed
Dublin187
Calcite | Level 5

Hi all,

I have a beginner's question : In a given dataset where we need to replace some values with another one, how do we write properly the if then statement in a data step? Or do I need to use another type of statement?

For example I need to update / replace the incorrect values for correct ones, but the log doesn't recognized the 'then' part of the statement when I write the same variable. 

if Gender = 'G' then gender  'F' ;

 if salary =. then salary = 25000;

Thank you very much

 

4 REPLIES 4
novinosrin
Tourmaline | Level 20

Can you post the log and output you are getting plz

Dublin187
Calcite | Level 5
if Gender = 'G' then gender 'F';
______
180

 

ERROR 180-322: Statement is not valid or it is used out of proper order.

novinosrin
Tourmaline | Level 20

You missed an = 

 

Correction

if Gender = 'G' then gender= 'F';

Must have been a typo

ballardw
Super User

@Dublin187 wrote:

Hi all,

I have a beginner's question : In a given dataset where we need to replace some values with another one, how do we write properly the if then statement in a data step? Or do I need to use another type of statement?

For example I need to update / replace the incorrect values for correct ones, but the log doesn't recognized the 'then' part of the statement when I write the same variable. 

if Gender = 'G' then gender  'F' ;

 if salary =. then salary = 25000;

Thank you very much

 


Well the first statement is missing the = to do assignment.

Can you describe what you mean by " but the log doesn't recognized the 'then' part of the statement when I write the same variable. ". This sounds like you may be getting a message in the log similar to "invalid data type" or conversion of values. If so, post the code and messages from the Log into a code box opened using the forum's {I} icon to preserve text formatting of messages or notes.

 

Another issue may arise if you have been using code similar to:

data have;
    set have;
    if variable='something' then variable='somethingelse';

run;

Using the  same dataset as input and output, while syntactically allowed often causes issues with results as it completely replaces the original data set and logic mistakes means that you need to go back to prior steps to use the "correct" data.

 

Another issue around character variables is that they have an assigned length. If the original variable is set to have a length of one character and you attempt to do something like:

if gender='F' then gender='female';

the characters past the length of the variable are discarded.

 

And another possible issue is that you have a numeric variable that has format applied to make it appear character for display. If that were the case then the "if" is likely requesting a comparison using a formatted value when you need to use the underlying numeric. And assigning a character value would fail.

 

You may need to run Proc Contents on your data set and show the result of that step along with the log of the data step.

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
  • 4 replies
  • 781 views
  • 0 likes
  • 3 in conversation