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
Can you post the log and output you are getting plz
ERROR 180-322: Statement is not valid or it is used out of proper order.
You missed an =
Correction
if Gender = 'G' then gender= 'F';
Must have been a typo
@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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.