Skip to content
Advertisement

SAS computing multiple new variables from one row

I have a dataset as listed below:

and I want to calculate 3 new variables (ERR_CODE, ERR_DETAIL, ERR_ID) according to behavior of certain columns.

  • If V1 is greater than 4 then ERR_CODE = A and ERR_DETAIL = “Out of range” and ERR_ID = [ID]_A
  • If V2 is greater than 4 then ERR_CODE = B and ERR_DETAIL = “Check Log” and ERR_ID = [ID]_B
  • If V3 is greater than 4 then ERR_CODE = C and ERR_DETAIL = “Fault” and ERR_ID = [ID]_C

Desired output table be like

I am using SAS 9.3 with EG 5.1. I have tried do-loops, arrays, if statements and case-when’s but it naturally skips to the next row to calculate when condition is met. But i want to calculate other met conditions fo each row.

I have managed to do it by creating seperate tables for each condition and then merge them. But that doesn’t seem an effective way if there are much conditions to work with.

My question is how can i manage to calculate other met conditions for each ID at once without calculating seperately? The output table’s row count will be more than the input as expected but for me it is not possible to achieve by applying case-when or if etc.

Thanks in advance and sorry if i am not clear.

Advertisement

Answer

Just use IF/THEN/DO blocks. Add an OUTPUT statement to write new observation for each error.

Results:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement