BookmarkSubscribeRSS Feed
s100
Calcite | Level 5

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!

9 REPLIES 9
Tom
Super User Tom
Super User

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;

s100
Calcite | Level 5

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!

Reeza
Super User

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;

Tom
Super User Tom
Super User

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;

felipeespi
Calcite | Level 5

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;

Reeza
Super User

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.

felipeespi
Calcite | Level 5

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.

Tom
Super User Tom
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 9 replies
  • 990 views
  • 0 likes
  • 4 in conversation