BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
frupaul
Quartz | Level 8

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.

 

Capture1.PNG

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:

Capture2.PNG

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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;

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

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;
kiranv_
Rhodochrosite | Level 12

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;
Kurt_Bremser
Super User

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.

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 729 views
  • 5 likes
  • 4 in conversation