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

I have a dataset called INPUT1 on which I want to act. And a dataset called INPUT2 which will serve purely as input.

 

More specifically I want to do the following.

 

DATA OUTPUT;

SET INPUT1;

if MARK=<ROW1_Variable1_from_INPUT2 then Tier=0;

else if MARK>ROW1_Variable1_from_INPUT2 and MARK=<ROW2_Variable1_from_INPUT2 then Tier=1;

else if MARK>ROW2_Variable1_from_INPUT2 and MARK=<ROW3_Variable1_from_INPUT2 then Tier=2;

else if MARK>ROW3_Variable1_from_INPUT2 and MARK=<ROW4_Variable1_from_INPUT2 then Tier=3;

else if MARK>ROW4_Variable1_from_INPUT2 then Tier=4;

 

How can I do this? If it matters at all, Variable1 is numeric and ordered.

 

I'm on SAS ENTERPRISE GUIDE 5.1

 

Thanks.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

There are several methods you an use to do this - merging the two datasets, SQL join, SQL lookup, hash table.  You have not posted any test data (in the form of datasteps) or what the output should be so can't provide acurate code.  Generally speaking its not a good idea to reference a data element (observation) by position in the dataset as this can change drastically on each run.  Assigning an order to the data, even if it just takes the automatic variable _n_ is better than nothing:

data output;
  merge input1 
             input1 (where=(ord=1) keep=v1 rename=variable1=v1)
             input1 (where=(ord=2) keep=v2 rename=variable1=v2)
...
  by <id_vars>;
run;

Or lookup:

proc sql;
  create table OUTPUT as
  select  VARIABLE1,
             (select VARIABLE1 from HAVE where ORD=1) as VAR1,
             (select VARIABLE1 from HAVE where ORD=2) as VAR2,
...
             case when VARIABLE1 le CALCULATED VAR1 then 0
                      when ...
                      else . end as TIER
  from    HAVE;
quit;

 

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

There are several methods you an use to do this - merging the two datasets, SQL join, SQL lookup, hash table.  You have not posted any test data (in the form of datasteps) or what the output should be so can't provide acurate code.  Generally speaking its not a good idea to reference a data element (observation) by position in the dataset as this can change drastically on each run.  Assigning an order to the data, even if it just takes the automatic variable _n_ is better than nothing:

data output;
  merge input1 
             input1 (where=(ord=1) keep=v1 rename=variable1=v1)
             input1 (where=(ord=2) keep=v2 rename=variable1=v2)
...
  by <id_vars>;
run;

Or lookup:

proc sql;
  create table OUTPUT as
  select  VARIABLE1,
             (select VARIABLE1 from HAVE where ORD=1) as VAR1,
             (select VARIABLE1 from HAVE where ORD=2) as VAR2,
...
             case when VARIABLE1 le CALCULATED VAR1 then 0
                      when ...
                      else . end as TIER
  from    HAVE;
quit;

 

sleretrano
Quartz | Level 8

How can I define ORD to display

1
2
3
4

Without doing it "manually"?

 

 

Thank you for your reply.

sleretrano
Quartz | Level 8

OK, got it. It's just

ORD=_n_

I didn't know about this.

 

 

I'm trying to make your second option work at the moment.

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 1252 views
  • 0 likes
  • 2 in conversation