Hi All,
I have the below data set. My objective to let the Name be the last variables in every row without changing the last variables. the new data set should have James as value of L6 in row 1, Lemar as value for L8 in row 2 and Thomas as value for L7 in row 3 as the arrows indicate. The data step which creates this is in step 1 below. The code I have written is titled step 2. The error message I get follows after the codes.
step 1:
data Ranking;
INFILE DATALINES DLM=',' missover;
length CustID Name $10 L1 $20 L2 $20 L3 $20 L4 $20 L5 $20 L6 $20 L7 $20 L8 $20;
input CustID Name $ L1 $ L2 $ L3 $ L4 $ L5 $ L6 $ L7 $ L8 $;
cards;
1,James,CEO,Department Head,Senior Manager,Manager, Senior Analyst, , ,
2,Lemar,Major,Vice President, CEO, Department Head, Senior Manager,Manager,Senior Analyst,
3,Thomas,Technical Chief, CEO, Department Head, Senior Manager, Manager, Senior Analyst,
;
Step 2:
data new;
set ranking;
array L{8} $;
do i=8 to 1 by -1;
if missing(L{i} ) then do;
L{i+1} = Name;
call missing(L{i});
leave;
end;
end;
run;
error message:
Though your idea is fine, i think you can do it simpler like this
data want(drop=idx);
set Ranking;
array L{8} $;
idx=whichc("", of L[*]);
L[idx]=Name;
run;
Though your idea is fine, i think you can do it simpler like this
data want(drop=idx);
set Ranking;
array L{8} $;
idx=whichc("", of L[*]);
L[idx]=Name;
run;
something like this should work. when L = 8 then your L{i+1} will become 9 and you do not have 9 elements hence the error array
subscript out of range.
data new;
set ranking;
array L{8} $;
do i=1 to 8;
if missing(L{i} ) then do;
L{i} = Name;
leave;
end;
end;
run;
You loop from 8 to 1, but use i+1 in the loop, so it tries to access element 9 in the first iteration which does not exist. That is a conceptual mistake.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.