hi,
i created this output below using couple datasteps with arrays. I'm struggling to do the following: return the ID number where the value increases month-over-month. In the example given below the output would be ID 16 (since in april12 it had gone up to 114 from 100)
I'm writing this in VBA without any issues, since the code loop through cells and compare the values from the previous record using index functions. I'm struggling transitioning this to SAS. any help is much appreciated.
ID Month Score
16 Jan12 112
16 Feb12 110
16 March12 100
16 April12 114
35 Jan12 120
35 Feb12 115
35 March 106
35 April 100
data have; input ID $ Month $ Score; cards; 16 Jan12 112 16 Feb12 110 16 March12 100 16 April12 114 35 Jan12 120 35 Feb12 115 35 March 106 35 April 100 ; run; data want; set have; if id eq lag(id) and dif(score) gt 0; run;
Ksharp
If your data comes as is, lag() or dif() will be sufficient:
data have;
input ID $ Month $ Score;
cards;
16 Jan12 112
16 Feb12 110
16 March12 100
16 April12 114
35 Jan12 120
35 Feb12 115
35 March 106
35 April 100
;
data want;
set have;
by id notsorted;
d=dif(score);
if not first.id and d>0;
keep id;
run;
Haikuo
data have; input ID $ Month $ Score; cards; 16 Jan12 112 16 Feb12 110 16 March12 100 16 April12 114 35 Jan12 120 35 Feb12 115 35 March 106 35 April 100 ; run; data want; set have; if id eq lag(id) and dif(score) gt 0; run;
Ksharp
thank you Hai and ksharp for the help. it worked great!
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.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.