BookmarkSubscribeRSS Feed
Madhav4114
Calcite | Level 5

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

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

What does your desired result look like?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
s_lassen
Meteorite | Level 14

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;

sas-innovate-2024.png

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.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 618 views
  • 0 likes
  • 5 in conversation