Skip to content

Instantly share code, notes, and snippets.

@statgeek
Last active August 16, 2023 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save statgeek/972120a19b583acc3a947b58b3177a41 to your computer and use it in GitHub Desktop.
Save statgeek/972120a19b583acc3a947b58b3177a41 to your computer and use it in GitHub Desktop.
SAS - Arrays - Finding Name from Indicator List
*https://communities.sas.com/t5/SAS-Programming/combining-multiple-columns/m-p/705769
/*This example illustrates how to combine variables from labels or variable names
from a list of indicator variables
Author: F. Khurshed
Date: 2020-12-14
*/
data have;
infile cards dlm='09'x;
input ID Caucasian AfricanAmerican EastAsian SouthEastAsian;
label AfricanAmerican='African American' EastAsian="East Asian"
SOuthEastAsian="Southeast Asian";
cards;
1 1 0 1 0
2 0 1 0 0
3 0 0 0 1
4 1 0 0 0
;
;
;;
run;
data want;
set have;
length list_race_label list_race_name $200.;
array _race(*) Caucasian--SouthEastAsian;
race_sum=sum(of _race(*));
if race_sum > 1 then
final_race="Multiple Race";
else
final_race=vname(_race(whichn(1, of _race(*))));
*list of race;
do i=1 to dim(_race);
if _race(i)=1 then
list_race_label=catx(", ", trim(list_race_label), vlabel(_race(i)));
if _race(i)=1 then
list_race_name=catx(", ", trim(list_race_label), vname(_race(i)));
end;
run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment