BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
agcetin
Fluorite | Level 6

Hello everybody,

I'd like to count flag by id and order and get the "target" as below. In summary, I want to put 1 when flag is 1 and add 1 when order increases. It shouldn't change when order is same. 

Could you help with solving this problem? 

 

 

idorderflagtarget
1100
1100
1200
1200
1211
1302
1311
1402
1402
1503
1503
1604
1604
2100
2100
2200
2200
2211
2302
2302
2411
2401
2502
2502
2502

 

 

data data01;

input id  order flag;

datalines;

1 1 0

1 1 0

1 2 0

1 2 0

1 2 1

1 3 0

1 3 1

1 4 0

1 4 0

1 5 0

1 5 0

1 6 0

1 6 0

2 1 0

2 1 0

2 2 0

2 2 0

2 2 1

2 3 0

2 3 0

2 4 1

2 4 0

2 5 0

2 5 0

2 5 0

;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
ndp
Quartz | Level 8 ndp
Quartz | Level 8

Try this:

 

data want;
    set data01;
    by id order;
    if first.id then target=0;
    if flag=1 then target=1;
    if first.order and target>0 and flag=0 then target+1;
run;

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

This is untested code



 data want;
    set have;
    by id order;
    if first.id then target=0;
    if flag=1 then target=1;
    if first.order then target+1;
run;

 

--
Paige Miller
agcetin
Fluorite | Level 6
Thanks for your reply PaigeMiller but it didn't work as I wanted.
PaigeMiller
Diamond | Level 26

If you want further help, you have to explain further.

--
Paige Miller
ndp
Quartz | Level 8 ndp
Quartz | Level 8

Try this:

 

data want;
    set data01;
    by id order;
    if first.id then target=0;
    if flag=1 then target=1;
    if first.order and target>0 and flag=0 then target+1;
run;
agcetin
Fluorite | Level 6
Thanks a lot ndp. it works as I want. Cheers.
Steelers_In_DC
Barite | Level 11

In my haste the code seems to have gotten away from me a little.  I won't be surprised to see a more elegant solution but this will work:

 

data want(drop=_:);
set data01;
by id order;
retain _target;
if first.id and flag = 0 then do; _target = 0;target = 0;end;
if flag = 1 then do; target = 1; _target = 1;end;
if first.order and _target = 1 then target + 1;
if flag = 1 then target = 1;
run;

agcetin
Fluorite | Level 6
it also works. Many thanks.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

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
  • 7 replies
  • 947 views
  • 2 likes
  • 4 in conversation