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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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?


 

View solution in original post

2 REPLIES 2
ballardw
Super User

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?


 

SAS_learneromg
Fluorite | Level 6
That did the trick! Thanks for that explanation. Don't know I forgot my set statement lol.

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
  • 2 replies
  • 16279 views
  • 1 like
  • 2 in conversation