BookmarkSubscribeRSS Feed
Miracle
Barite | Level 11

Hi everyone.

I'm wondering how can I generate the variable final given I have the dataset with variable id, flag and total more elegantly than what I've done(below)?

Thank you very much and happy Easter!

     id      flag     total    final

       1         0         4       3

       1         .         4        3

       1         .         4        3

       1         1         4       3

       2         1         2        2

       2         .         2         2

       3         .         5         2

       3         1         5        2

       3         0         5        2

       3         0         5        2

       3         0         5        2

       4         .         1         1

       5         .         2         2

       5         .         2         2

My code is:

data b;

  set a;

  by id;

  retain total2 0;

  if first.id then total2=total;

  if flag=0 then total2=total2-1;

run;

proc sql;

select *, min(total2) as final

from b

group by id;

quit;

1 REPLY 1
AncaTilea
Pyrite | Level 9

How about this:

proc sql;

    select *,sum(flag ne 0) as final2

    from a

    group by id;

quit;

Smiley Happy

Good luck!

Anca.

PS: and if you want a table, then add the

proc sql;
     create table your_table as

    select *,sum(flag ne 0) as final2

    from a

    group by id;

quit;

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1 reply
  • 1216 views
  • 1 like
  • 2 in conversation