Help using Base SAS procedures

Array Code

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

Array Code

Hi,

How can I scan through an array and put the last value of the array into another variable..

Have

value1    value2   value3  value4   value5 value6

xa          xm       bb

cc            ad       mn       xy

xo

xe           an        th        ty          pb     kl

WANT:

new

bb

xy

xo

kl

Thanks


Accepted Solutions
Solution
‎05-27-2013 09:07 PM
Respected Advisor
Posts: 3,124

Re: Array Code

I suppose there maybe better approaches, but you can always do it reversely:

data have;

infile cards truncover;

input (  value1    value2   value3  value4   value5 value6) (:$2.);

array v value6 value5 value4 value3 value2 value1;

do over v;

  if not missing (v) then do; new=v;leave;end;

  end;

cards;

xa          xm       bb

cc            ad       mn       xy

xo

xe           an        th        ty          pb     kl

;

Haikuo

UPdate: here is another option, better, in a way that you don't need to define array() elements explicitly.

data have;

infile cards truncover;

input (  value1    value2   value3  value4   value5 value6) (:$2.);

array v value:;

new=v(dim(v)-cmiss(of v(*)));

cards;

xa          xm       bb

cc            ad       mn       xy

xo

xe           an        th        ty          pb     kl

;

View solution in original post


All Replies
Solution
‎05-27-2013 09:07 PM
Respected Advisor
Posts: 3,124

Re: Array Code

I suppose there maybe better approaches, but you can always do it reversely:

data have;

infile cards truncover;

input (  value1    value2   value3  value4   value5 value6) (:$2.);

array v value6 value5 value4 value3 value2 value1;

do over v;

  if not missing (v) then do; new=v;leave;end;

  end;

cards;

xa          xm       bb

cc            ad       mn       xy

xo

xe           an        th        ty          pb     kl

;

Haikuo

UPdate: here is another option, better, in a way that you don't need to define array() elements explicitly.

data have;

infile cards truncover;

input (  value1    value2   value3  value4   value5 value6) (:$2.);

array v value:;

new=v(dim(v)-cmiss(of v(*)));

cards;

xa          xm       bb

cc            ad       mn       xy

xo

xe           an        th        ty          pb     kl

;

Super Contributor
Posts: 1,040

Re: Array Code

Thanks Hi,

Unfortunately ,the system is not showing the Correct option..I will update it a lil later

Regards

Super Contributor
Posts: 1,040

Re: Array Code

hi,

I was curious why did you put the array items in the reverse order in the first method????????/

Thanks

Respected Advisor
Posts: 3,124

Re: Array Code

So when looping through array, it starts from value6 to value1. Of course you don't have to do that, cause you can always loop it from the END to START, it didn't occur to me until I posted up.

Super Contributor
Posts: 1,040

Re: Array Code

I was thinking if array  goes from end to start it would give us

new

xa        

cc          

xo

xe    

Thnkx    

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 210 views
  • 3 likes
  • 2 in conversation