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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1937 views
  • 0 likes
  • 2 in conversation