Help using Base SAS procedures

Array question

Reply
Occasional Contributor
Posts: 11

Array question

Hello,

ID

a1a2a3a4a5xy
11110113
21010124
30101035
41111115

My dataset looks like this. I created an array for a1-a5. The purpose is to select the records that all the a(i) values are equal to 1 ( i = x to y).

I am a new SAS user. Any suggestions would be appreciated.

Thanks in advance!

Super User
Super User
Posts: 6,502

Re: Array question

If you only want to look at the values whose index is between x and y then you will probably need to code a loop.

Since you only want to keep those will all one's you can just DELETE when you see something that is not 1. This will end that iteration of the data step without outputting the record and immediately go to the next observation in the source.

data want ;

  set have ;

  array a a1-a5;

  do i=x to y; if a(i) ne 1 then delete; end;

run;

Occasional Contributor
Posts: 11

Re: Array question

Thanks, Tom! That's helpful.

But in my real dataset, some people may have multiple records/observations. The requirement ( a(i)=1 ) may just apply to one of their records. If I use 'delete' function, that will only delete one observation per person. So I think output all the eligible people might be more easier. Any idea how to output those ids? Thanks!

Super User
Posts: 17,912

Re: Array question

What if the ID has situations where all a(i) are one in one observation and not in the other?

data want_id;

set have;

array a a1-a5;

if max(of a(*))=min(of a(*)) and max(of a(*))=1 then output;

keep id;

run;

proc sql;

     create table want as

select * from have

where id in (select distinct id from want_id);

quit;

Super User
Super User
Posts: 6,502

Re: Array question

So you want to keep all observations for any ID that has at least one record where all values from a(X) to a(Y) are one?

Try using a two DOW loops.  The first will process all of the observations for that level of ID and set the flag ANY1 to true if any of them have all values of one.

The second DOW loop will process the same block of records and again and output them based on the ANY1 flag.

data want ;

  any1 = 0;

  do until (last.id);

    set have ;

    by id;

    array a a1-a5;

     anynot1 =0;

    do i=x to y;

      if a(i) ne 1 then anynot1=1;

    end;

     if not anynot1 then any1=1;

  end;

  do until (last.id);

    set have ;

    by id;

    if any1 then output;

  end;

run;

Occasional Contributor
Posts: 8

Re: Array question

Hi Tom I was wondering if you could help me with a simple but yet complex task that's driving me nuts.

My data set in excel

dogcatmicecamelpigeoncowgoatfishhorsedonkeymonkeylionrat
970.2525340.572243184345633122315440.69

I want to create two variables 'animals' and 'weight' to match each animal in columns with respective weight without modifying the data. Just by using proc import and or proc sort.

proc import datafile='E:\Copy of final examination.xls' out=Animals dbms=xls replace;sheet=animals;run;

Super User
Posts: 17,912

Re: Array question

You should post your qeustion in a new thread.

And what do you mean by just using import and sort? You can't use tranpose or a data step?

I'm not sure its possible with just an import/transpose.

Occasional Contributor
Posts: 8

Re: Array question

Thanks Reeza.

We're not to use transpose for this specific question - I could try data step but I'm unsure of how to go about it.

Super User
Super User
Posts: 6,502

Re: Array question

If your want to figure it out yourself look for a SAS function that you could with an array in a data step.

Super User
Posts: 17,912

Re: Array question

Ask a Question
Discussion stats
  • 9 replies
  • 273 views
  • 0 likes
  • 4 in conversation