Desktop productivity for business analysts and programmers

Use data from another table

Accepted Solution Solved
Reply
Contributor
Posts: 46
Accepted Solution

Use data from another table

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.

 

 


Accepted Solutions
Solution
‎05-20-2016 11:42 AM
Super User
Super User
Posts: 7,671

Re: Use data from another table

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


All Replies
Solution
‎05-20-2016 11:42 AM
Super User
Super User
Posts: 7,671

Re: Use data from another table

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;

 

Contributor
Posts: 46

Re: Use data from another table

How can I define ORD to display

1
2
3
4

Without doing it "manually"?

 

 

Thank you for your reply.

Contributor
Posts: 46

Re: Use data from another table

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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