Help me find solutions for below problem
Date. Newkey. Flag
Dec16. 13. 0
Jan17. 13. 1
Feb17. 13. 1
Mar17. 13. 1
Need to find total cases as to how many times the flag switched from 0 to 1 in above table
What does your desired result look like?
Not tested (pop test data in the form of a data step please):
data want;
set have;
retain num_times 0;
if lag(flag)=0 and flag=1 then num_times=sum(num_times,1);
run;
Last record would have your total number of times. You could also do it via sql to join lowest previous date to current record as well.
Please go back to your ORIGINAL post and provide a subject line that describes the problem. A subject line of "SAS Code" could apply to 99% of the posts here, and so has no value in describing the problem. Help us out, please; we're trying to help you.
If you just need the count, you can do like this:
data want;
retain count -1; /* the first record should set count to 0 */
set have end=done;
by flag notsorted;
count+first.flag;
if done;
keep count;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Get started using SAS Studio to write, run and debug your SAS programs.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.