BookmarkSubscribeRSS Feed
kk13
Calcite | Level 5

I have multi dimensional array of numeric numbers.  I would like to create separate multi dimensional array that shows the first observed unique numbers.

 

data have1;
input a1_1-a1_4 b1_1-b1_4 c1_1-c1_4;
datalines;
2000 2001 2002 . 2001 2004 . . 2002 2004 2005 20006
2000 2002 2003 2004 2003 2004 . . 2002 2005 2006 .
;

data want1;
input fa1_1-fa1_4 fb1_1-fb1_4 fc1_1-fc1_4;
datalines;
2000 2001 2002 . . 20004 . . . . 2005 2006
2000 2002 2003 2004 . . . . . 2005 2006 .
;

2 REPLIES 2
andreas_lds
Jade | Level 19

You may want to check the documentation to verify that the concept of arrays in sas matches your notion of arrays.

ballardw
Super User

@kk13 wrote:

I have multi dimensional array of numeric numbers.  I would like to create separate multi dimensional array that shows the first observed unique numbers.

 

data have1;
input a1_1-a1_4 b1_1-b1_4 c1_1-c1_4;
datalines;
2000 2001 2002 . 2001 2004 . . 2002 2004 2005 20006
2000 2002 2003 2004 2003 2004 . . 2002 2005 2006 .
;

data want1;
input fa1_1-fa1_4 fb1_1-fb1_4 fc1_1-fc1_4;
datalines;
2000 2001 2002 . . 20004 . . . . 2005 2006
2000 2002 2003 2004 . . . . . 2005 2006 .
;


Please be careful in creating your example data. You have values that apparently change from 20006 to 2006 and 20004 to 2004

Assuming none of the values are supposed to have 3 zeroes this works for the example:

data have1;
input a1_1-a1_4 b1_1-b1_4 c1_1-c1_4;
datalines;
2000 2001 2002 . 2001 2004 . . 2002 2004 2005 2006
2000 2002 2003 2004 2003 2004 . . 2002 2005 2006 .
;


data want;
   set have1;
   array f a1_1-a1_4 b1_1-b1_4 c1_1-c1_4;
   do i=1 to dim(f);

          if   whichn(f[i],of f(*) )  ne i
          then f[i]=.;

   end;
   drop i;
run;

Arrays are a way to do the same to multiple variables.

The function WHICHN, and a similar WHICHC for character values, returns the first position in a list of values where the first value is the one to search for and everything following is the list. Example:

   Whichn(5, 1, 2, 5) returns 3 because 5 is the third element in the list 1,2,5.

 A short hand way to say "use an array as the list" is "of arrayname(*)". The * is "all elements".

 

You refer to a specific variable in an array using arrayname(index) . The () could be [ ] or { }.

So the above code finds the first position , or  index, of the current variable's value and if that position is different than the current index it is not the first occurrence of the value and uses the "f[i] = ." to set the value to missing.

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
  • 2 replies
  • 346 views
  • 1 like
  • 3 in conversation