I have a big dataset of medical test results that includes a "jurisdiction" variable for the state where the person had their testing done. SO for most people the value of jurisdiction is FL, GA. Except for people who were tested in New York City, Los Angeles and San Francisco, they have values of "NYC" "LAX" and "SF". For my purposes, I just want state level data. Is there was was to change "LAX" and "SF" to "CA", and "NYC to "NY" without having to create a new variable. This is the code I tried:
data test_output;
if jurisdiction in "LAX" then jurisdiction = "CA";
if jurisdiction in "SF" then jurisdiction = "CA";
if jurisdiction in "NYC" then jurisdiction = "NY";
run;
That didn't work so I must be missing something. Any tips?
Your code does not show a SET statement for a data source. That is the first thing.
The IN operator requires ( ) around the values. The log would show that if you supply a data set.
Example:
180 data junk; 181 set sashelp.class; 182 if sex in "F" then sex="A"; --- 390 200 76 ERROR 390-185: Expecting an relational or arithmetic operator. ERROR 200-322: The symbol is not recognized and will be ignored. ERROR 76-322: Syntax error, statement will be ignored. 183 run;
Since you have two values for CA I would suggest something similar to this to take advantage of the way the IN operator works:
data test_output; set yourdatagoeshere; if jurisdiction in ("LAX" "SF") then jurisdiction = "CA"; else if jurisdiction = "NYC" then jurisdiction = "NY"; run;
I used an ELSE because it is a good habit to get into with this sort of recoding as it is possible to have a number of changes and you might end up undoing a previous assignment. Also more complex conditions using multiple variables can get you into that mess quickly.
The SELECT/WHEN/END construct may be easier to follow and write if you have more than one or two reassignments.
@SAS_learneromg wrote:
I have a big dataset of medical test results that includes a "jurisdiction" variable for the state where the person had their testing done. SO for most people the value of jurisdiction is FL, GA. Except for people who were tested in New York City, Los Angeles and San Francisco, they have values of "NYC" "LAX" and "SF". For my purposes, I just want state level data. Is there was was to change "LAX" and "SF" to "CA", and "NYC to "NY" without having to create a new variable. This is the code I tried:
data test_output;
if jurisdiction in "LAX" then jurisdiction = "CA";
if jurisdiction in "SF" then jurisdiction = "CA";
if jurisdiction in "NYC" then jurisdiction = "NY";
run;
That didn't work so I must be missing something. Any tips?
Your code does not show a SET statement for a data source. That is the first thing.
The IN operator requires ( ) around the values. The log would show that if you supply a data set.
Example:
180 data junk; 181 set sashelp.class; 182 if sex in "F" then sex="A"; --- 390 200 76 ERROR 390-185: Expecting an relational or arithmetic operator. ERROR 200-322: The symbol is not recognized and will be ignored. ERROR 76-322: Syntax error, statement will be ignored. 183 run;
Since you have two values for CA I would suggest something similar to this to take advantage of the way the IN operator works:
data test_output; set yourdatagoeshere; if jurisdiction in ("LAX" "SF") then jurisdiction = "CA"; else if jurisdiction = "NYC" then jurisdiction = "NY"; run;
I used an ELSE because it is a good habit to get into with this sort of recoding as it is possible to have a number of changes and you might end up undoing a previous assignment. Also more complex conditions using multiple variables can get you into that mess quickly.
The SELECT/WHEN/END construct may be easier to follow and write if you have more than one or two reassignments.
@SAS_learneromg wrote:
I have a big dataset of medical test results that includes a "jurisdiction" variable for the state where the person had their testing done. SO for most people the value of jurisdiction is FL, GA. Except for people who were tested in New York City, Los Angeles and San Francisco, they have values of "NYC" "LAX" and "SF". For my purposes, I just want state level data. Is there was was to change "LAX" and "SF" to "CA", and "NYC to "NY" without having to create a new variable. This is the code I tried:
data test_output;
if jurisdiction in "LAX" then jurisdiction = "CA";
if jurisdiction in "SF" then jurisdiction = "CA";
if jurisdiction in "NYC" then jurisdiction = "NY";
run;
That didn't work so I must be missing something. Any tips?
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.