Flagging an error for only specific variables

Reply
Occasional Contributor
Posts: 6

Flagging an error for only specific variables

Hello,

 

I am trying to write a code for a data set that will always be changing. In my code, there is a macro where you put in the province code, and then a raw data file is imported and checked for errors. I want the program to flag it as an error, if there is an ABC code that is not an option for that particular province. For example, if the raw data file is say AB and the ABC code is not in 'A" or "KK" than I want that to be flagged as an error. So basically, what counts as an error will change based on the province code. Or if for example the "ABC Code" is R, and it is SK, that is no error, but if it is AB that is an error. And this table is always changing. But in essence, I want it to be an error, only if there is not an "ABC Code" for the specific province being checked.

 

It looks something like this:

 Province Code

ABC Code

 AB

 A

 AB

 KK

 BC

 C

SK

 R

NS

 T

Respected Advisor
Posts: 3,040

Re: Flagging an error for only specific variables

Posted in reply to rcamphaug
if province_code='AB' and abc_code not in ('A','KK') then flag=1;
else if province_code='SK' and abc_code not in ( ... ) then flag=1; /* You have to fill in the abc_codes for SK */
--
Paige Miller
Occasional Contributor
Posts: 6

Re: Flagging an error for only specific variables

Posted in reply to PaigeMiller

Hello! thanks for the reply Smiley Happy I was originally thinking I would do something like that but the problem is that the table is actually huge and constantly changing, so I need to somehow get around writing all of the 'ABC codes'

Super User
Posts: 23,754

Re: Flagging an error for only specific variables

Posted in reply to rcamphaug

A user defined format is a great idea here. 

You can make one for each province, with OTHER/MISSING flagged as needed, and then any that don't meet your list will become ERROR that you can flag or modify as needed.

 

You can create formats from a data set so this can be entirely dynamic as your lists changes as needed. 

 

Lets assume you have a macro variable PROV that's set to AB, and you then have a format called AB_valid_fmt, you can use PUTC to check the values.

 

check = putc(ABC, "&PROV_valid_fmt.");
if check = "ERROR" then ...
Super User
Posts: 10,268

Re: Flagging an error for only specific variables

Posted in reply to rcamphaug

Create a table with the valid pairings, and do a join/merge with that table. If abc_code is not missing and there's no match, you have an error.

That way you get intelligent code (does not need to be changed) out of intelligent data.

Alternatively, you can create a format out of that control dataset (make a compound value out of province and abc_code), and apply that to a compound of province and abc_code in your main dataset. Then you don't need the join/merge.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 4 replies
  • 93 views
  • 0 likes
  • 4 in conversation