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').
Explanation : First matching method using a classic IF-ELSE conditional structure to identify the corresponding column.
Copied!
data ratings_match;
set ratings;
length rating_match $8;
if rating = moodys then rating_match = 'moodys';
else if rating = s_and_p then rating_match = 's_and_p';
else if rating = fitch then rating_match = 'fitch';
else rating_match = 'no match';
run;
1
DATA ratings_match;
2
SET ratings;
3
LENGTH rating_match $8;
4
5
IF rating = moodys THEN rating_match = 'moodys';
6
ELSEIF rating = s_and_p THEN rating_match = 's_and_p';
7
ELSEIF rating = fitch THEN rating_match = 'fitch';
8
ELSE rating_match = 'no match';
9
RUN;
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!
data ratings_match;
set ratings;
/* WHICHC talar om variabelnumret man fr trff p, baserat p ordningen
man raddar upp variablerna i p samma stt som COALESCEC.
Finns bara trff i en variabel spelar ordningen ingen roll.
Prioordning (i de fall man fr trff p flera):
1. Moodys
2. S&P
3. Fitch */
variable_match_number = whichc(rating, moodys, s_and_p, fitch);
run;
1
DATA 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.
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!
data ratings_match;
set ratings;
length rating_match $8;
/* Array fr att peka p de tre variablerna med vrden,
mste ha samma ordning som WHICHC */
array rating_agency [3] moodys s_and_p fitch;
variable_match_number = whichc(rating, moodys, s_and_p, fitch);
/* Fr man trff i ngon variabel g vidare och plocka fram namnet p variabeln */
if variable_match_number then rating_match = vname(rating_agency[variable_match_number]);
else rating_match = 'no match';
run;
1
DATA ratings_match;
2
SET ratings;
3
LENGTH rating_match $8;
4
/* Array fr att peka p de tre variablerna med vrden,
/* 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';
13
RUN;
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!
/* ndrar ordningen p WHICHC och inte arrayen, blir helt tokigt.
r det lnga listor kan man med frdel speca ordningen i en
makrovariabel s man bara gr det 1 gng. */
data ratings_match_error;
set ratings;
length rating_match $8;
array rating_agency [3] moodys s_and_p fitch;
variable_match_number = whichc(rating, s_and_p, fitch, moodys);
if variable_match_number then rating_match = vname(rating_agency[variable_match_number]);
else rating_match = 'no match';
run;
1
/* ndrar ordningen p WHICHC och inte arrayen, blir helt tokigt.
2
r det lnga listor kan man med frdel speca ordningen i en
IF variable_match_number THEN rating_match = vname(rating_agency[variable_match_number]);
12
ELSE rating_match = 'no match';
13
RUN;
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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.