Published on :
Data Manipulation CREATION_INTERNE

Value Match Search and Variable Name Retrieval

This code is also available in: Deutsch Español Français
Awaiting validation
The script first generates a table containing ratings. It then compares a target 'rating' column with three agency columns (moodys, s_and_p, fitch). Several techniques are presented: classic IF-ELSE conditions, using the WHICHC function to find the index of the matching column, and an advanced method combining WHICHC, ARRAY, and VNAME to dynamically retrieve the name of the corresponding variable. A final step warns against misalignment between the order of WHICHC arguments and the ARRAY.
Data Analysis

Type : CREATION_INTERNE


Data is generated directly in the code via assignment and OUTPUT statements.

1 Code Block
DATA STEP Data
Explanation :
Creation of the 'ratings' table containing a target 'rating' value and three comparison columns ('moodys', 's_and_p', 'fitch').
Copied!
1DATA ratings;
2 LENGTH rating $4;
3 rating = 'A+';
4 moodys = 'Baa2';
5 s_and_p = 'BBB+';
6 fitch = 'A+';
7 OUTPUT;
8 rating = 'Aa3';
9 moodys = 'Aa3';
10 s_and_p = 'AAA';
11 fitch = 'AA-';
12 OUTPUT;
13 rating = 'c';
14 moodys = 'Aa3';
15 s_and_p = 'AAA';
16 fitch = 'AA-';
17 OUTPUT;
18 rating = 'D-';
19 moodys = 'Aa3';
20 s_and_p = 'D-';
21 fitch = 'D-';
22 OUTPUT;
23RUN;
2 Code Block
DATA STEP
Explanation :
First matching method using a classic IF-ELSE conditional structure to identify the corresponding column.
Copied!
1DATA ratings_match;
2 SET ratings;
3 LENGTH rating_match $8;
4 
5 IF rating = moodys THEN rating_match = 'moodys';
6 ELSE IF rating = s_and_p THEN rating_match = 's_and_p';
7 ELSE IF rating = fitch THEN rating_match = 'fitch';
8 ELSE rating_match = 'no match';
9RUN;
3 Code Block
DATA STEP
Explanation :
Use of the WHICHC function to return the index (position) of the first matching variable in the provided list.
Copied!
1DATA ratings_match;
2 SET ratings;
3 /* WHICHC talar om variabelnumret man fr trff p, baserat p ordningen
4 man raddar upp variablerna i p samma stt som COALESCEC.
5 Finns bara trff i en variabel spelar ordningen ingen roll.
6 Prioordning (i de fall man fr trff p flera):
7 1. Moodys
8 2. S&P
9 3. Fitch */
10 variable_match_number = whichc(rating, moodys, s_and_p, fitch);
11RUN;
4 Code Block
DATA STEP
Explanation :
Combination of an ARRAY, the WHICHC function, and the VNAME function. The index returned by WHICHC is used to access the array element, and VNAME retrieves the source variable name.
Copied!
1DATA ratings_match;
2 SET ratings;
3 LENGTH rating_match $8;
4 /* Array fr att peka p de tre variablerna med vrden,
5 mste ha samma ordning som WHICHC */
6 array rating_agency [3] moodys s_and_p fitch;
7
8 variable_match_number = whichc(rating, moodys, s_and_p, fitch);
9
10 /* Fr man trff i ngon variabel g vidare och plocka fram namnet p variabeln */
11 IF variable_match_number THEN rating_match = vname(rating_agency[variable_match_number]);
12 ELSE rating_match = 'no match';
13RUN;
5 Code Block
DATA STEP
Explanation :
Demonstration of a logical error case: the order of variables in WHICHC differs from the order in the ARRAY, which leads to retrieving the wrong variable name via VNAME.
Copied!
1/* ndrar ordningen p WHICHC och inte arrayen, blir helt tokigt.
2 r det lnga listor kan man med frdel speca ordningen i en
3 makrovariabel s man bara gr det 1 gng. */
4DATA ratings_match_error;
5 SET ratings;
6 LENGTH rating_match $8;
7 array rating_agency [3] moodys s_and_p fitch;
8
9 variable_match_number = whichc(rating, s_and_p, fitch, moodys);
10
11 IF variable_match_number THEN rating_match = vname(rating_agency[variable_match_number]);
12 ELSE rating_match = 'no match';
13RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.