BookmarkSubscribeRSS Feed
sashelp123
Calcite | Level 5

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

4 REPLIES 4
PaigeMiller
Diamond | Level 26
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
sashelp123
Calcite | Level 5

Hello! thanks for the reply 🙂 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'

Reeza
Super User

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 ...
Kurt_Bremser
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 570 views
  • 0 likes
  • 4 in conversation