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.
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;
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;
How can I define ORD to display
1
2
3
4
Without doing it "manually"?
Thank you for your reply.
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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.