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

Good Morning!

 

I am struggling to find a solution to create a more concise definition for a created variable.  As of now, am I using the below CASE statement:

 

CASE
    WHEN administrative_status IN ("BK", "13") OR remark_code1 = "BK" OR remark_code2 = "BK" OR remark_code3 = "BK" OR remark_code4 = "BK OR 
remark_code5 = "BK" OR remark_code6 = "BK" OR remark_code7 = "BK" OR remark_code8 = "BK" OR remark_code9 = "BK" OR remark_code10 = "BK" THEN "BK" ELSE ""
END AS LEGAL_INDICATOR_c ,

INDEXC will not work because there are multiple codes with 'B' or 'K' characters, and INDEXW can only search one variable at a time.  I am sure there is an easy solution that I am missing (I mean, there has to be, right??).

 

I know that this is INCORRECT usage but something like:

 

FIND("BK", remark_code1, remark_code2, remark_code3, remark_code4, remark_code5, remark_code6, remark_code7, remark_code8, remark_code9, remark_code10)

 

Thanks for your help!  Any feedback is appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

For that specific test the WHICHC() function could be useful.  

if whichc('BK',of administrative_status remark_code1-remark_code10)
or administrative_status = "13" then LEGAL_INDICATOR_C = 'BK';
else LEGAL_INDICATOR_C = '  ';

Note that it is a little harder if you are required to use SQL since SAS does not support variable lists in SQL code. You will have to list all 10 of the remark_code variables.

CASE
    WHEN administrative_status = "13" then 'BK'
    WHEN whichc('BK',administrative_status,remark_code1,remark_code2,remark_code3
               ,remark_code4,remark_code5,remark_code6,remark_code7,remark_code8
               ,remark_code9,remark_code10) then 'BK'
    ELSE '  '
END AS LEGAL_INDICATOR_C

View solution in original post

4 REPLIES 4
s_lassen
Meteorite | Level 14

Maybe something like this will work:

case
  when indexw(catx(' ',administrative_status,of remark_code1-remark_code10),'BK') or administrative_status='13' then 'BK' 
  else '' 
end as legal_indicator_c
FreelanceReinh
Jade | Level 19

Hello @GBL__,

 

Variable lists or arrays, which could make this definition more concise, are not available in PROC SQL. Here are three options:

 

Option 1: Use a DATA step, e.g.:

data want;
set have;
array remark_code[10];
if administrative_status in ('BK', '13') or 'BK' in remark_code
  then legal_indicator_c='BK';
  else legal_indicator_c=' ';
run;

 

Option 2: Create the repetitive code with a DATA or PROC SQL step. In your example (with only 10 repetitions) the resulting code will not be (much) shorter, though.

 

Option 3: Consider transposing your data to obtain a "long" data structure where the 10 values of remark_code1-remark_code10 would be stored in 10 observations of a single variable remark_code. This structure would be more suitable for PROC SQL as you could apply summary functions together with a GROUP BY clause (grouping those 10 observations).

Tom
Super User Tom
Super User

For that specific test the WHICHC() function could be useful.  

if whichc('BK',of administrative_status remark_code1-remark_code10)
or administrative_status = "13" then LEGAL_INDICATOR_C = 'BK';
else LEGAL_INDICATOR_C = '  ';

Note that it is a little harder if you are required to use SQL since SAS does not support variable lists in SQL code. You will have to list all 10 of the remark_code variables.

CASE
    WHEN administrative_status = "13" then 'BK'
    WHEN whichc('BK',administrative_status,remark_code1,remark_code2,remark_code3
               ,remark_code4,remark_code5,remark_code6,remark_code7,remark_code8
               ,remark_code9,remark_code10) then 'BK'
    ELSE '  '
END AS LEGAL_INDICATOR_C
GBL__
Quartz | Level 8
I thought it might be something via WHICHC or CHOOSEC, but wasn't sure how to implement either. Thank you for your help!!

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
  • 4 replies
  • 1001 views
  • 5 likes
  • 4 in conversation