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

Hello,

I am trying to create a field for total number in a sequence based on ID and DateCreated. I have already created "SeqNum" based on ID and DateCreated, but now I want to get total number (TotalNum) in that sequence: 

 

IDDateCreatedSeqNumTotalNum
101mar202013
101mar202023
101mar202033
201apr202011
201sep202011
308aug202014
308aug202024
308aug202034
308aug202044

 

I am very new to SAS, so I appreciate any help you can provide. Thank you so much!

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20


data have;
input ID	DateCreated :date9.	SeqNum;*	TotalNum;
format DateCreated date9.;	
cards;
1	01mar2020	1	3
1	01mar2020	2	3
1	01mar2020	3	3
2	01apr2020	1	1
2	01sep2020	1	1
3	08aug2020	1	4
3	08aug2020	2	4
3	08aug2020	3	4
3	08aug2020	4	4
;

proc sql;
 create table want as
 select *, max(seqnum) as totalnum
 from have
 group by id, DateCreated
 order by id, DateCreated,SeqNum;
quit;


View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20


data have;
input ID	DateCreated :date9.	SeqNum;*	TotalNum;
format DateCreated date9.;	
cards;
1	01mar2020	1	3
1	01mar2020	2	3
1	01mar2020	3	3
2	01apr2020	1	1
2	01sep2020	1	1
3	08aug2020	1	4
3	08aug2020	2	4
3	08aug2020	3	4
3	08aug2020	4	4
;

proc sql;
 create table want as
 select *, max(seqnum) as totalnum
 from have
 group by id, DateCreated
 order by id, DateCreated,SeqNum;
quit;


novinosrin
Tourmaline | Level 20

Or-->


data want;
 do _n_=1 by 1 until(last.id);
   set have;
   by id;
 end;
 totalnum=seqnum;
 do _n_=1 to _n_;
   set have;
   output;
 end;
run;

mkeintz
PROC Star

The double do until (last.id), as presented by @novinosrin is useful and should be part of every sas programmer's repertoire.

 

But in this case, simpler code does exactly the same thing.  And it offloads more of the logic construction from the programmer to the sas compiler: 

 

data want;
  set have (in=in1) have (in=in2);
  by id;
  if first.id then totalnum=0;
  totalnum+in1;
  if in2;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
skhan9
Fluorite | Level 6
Thank you for the quick response and solutions, novinosrin and mkeintz! I tried the option using proc sql and it worked! Really appreciate your help with this!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 730 views
  • 0 likes
  • 3 in conversation