I am having some trouble getting a proc format and data pull to work. First, here is the code for the format I am creating:
data main_code;
length fmtname $32 type $1 start $8 label $20 hlo $1;
keep fmtname type start label hlo ;
type='C';
hlo='M';
set lib.code_data ;
fmtname='source';
start=code;
label=source_colors;
output;
run;
proc sort nodupkey ; by fmtname start label ; run;
proc format cntlin=main_code; run;
The above code is meant to create a format based on a .csv I have already imported into SAS called code_data. The "source_colors" column of the .csv contains four distinct labels: Red, Green, Blue, and Yellow. Each label corresponds to a code in the .csv, and there are instances where a code will appear for more than one label.
The following code is the data pull I am trying to run after this format:
data lib.new_data (keep = red: green: blue: yellow: i10_pr1-i10_pr31);
set lib.old_data;
array proc_cd_{*} i10_pr1-i10_pr31;
red=0;
green=0;
blue=0;
yellow=0;
do i=1 to 31;
if put(proc_cd_{i}, $source.)= "Red" then do;
red=1;
red_qual_pr=proc_cd_{i};
red_qual_pr_pos=(i);
end;
end;
do i=1 to 31;
if put(proc_cd_{i}, $source.)= "Green" then do;
green=1;
green_qual_pr=proc_cd_{i};
green_qual_pr_pos=(i);
end;
end;
do i=1 to 31;
if put(proc_cd_{i}, $source.)= "Blue" then do;
blue=1;
blue_qual_pr=proc_cd_{i};
blue_qual_pr_pos=(i);
end;
end;
do i=1 to 31;
if put(proc_cd_{i}, $source.)= "Yellow" then do;
yellow=1;
yellow_qual_pr=proc_cd_{i};
yellow_qual_pr_pos=(i);
end;
end;
The primary problem here is that the do loops do not appear to work. "Red" and "Green" should be showing up in the final dataset with a similar amount of codes, but what ends up happening is that if a code shows up for Red and Green, this data pull is skipping Red and only giving Green a value of 1. I need it to give both Red and Green values of 1 any time that label appears in the $source format.
One workaround I figured out was to create four separate formats for each of red, green, blue, and yellow, but this is inefficient and I was wondering what I can fix to get just the single format above to work for all four labels?
I know this is probably a mess of an explanation, so please feel free to ask questions and I will try to clarify as best as possible.
Thanks for any help!