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

Hello all,

 

Please see the dataset below:

IDSVCDATE
122016
122016
122016
132016
182016
272016
272016
282016
2102016

 

What I want is:

IDSVCDATECOUNT
1220163
1302161
1820161
2720162
2820161
21020161
 
 
Can someone please help me code this?
 
Thanks!
 
 
 
 
 
 
 
 
 
 

 

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

This is best done in SQL:

data have;
input ID $ 1 SVCDATE;
cards;
1	22016
1	22016
1	22016
1	32016
1	82016
2	72016
2	72016
2	82016
2	102016
;
run;

proc sql;
  create table want as 
  select ID
         ,SVCDATE
         ,count(*) as Count
  from have
  group by ID
          ,SVCDATE
  ;
quit;

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26
proc freq data=have;
    tables id*svdate/list;
run;

Please, in the future do not provide data as screen captures. Provide the data according to these instructions.

--
Paige Miller
raajdesaii
Fluorite | Level 6
This does not get me what I have in the output.

I want to know how many unique date counts are by each id. For e.g. ID1 has 3 counts for 22016, ID1 has 1 count for 32016. I want to do this in a datastep.
SASKiwi
PROC Star

This is best done in SQL:

data have;
input ID $ 1 SVCDATE;
cards;
1	22016
1	22016
1	22016
1	32016
1	82016
2	72016
2	72016
2	82016
2	102016
;
run;

proc sql;
  create table want as 
  select ID
         ,SVCDATE
         ,count(*) as Count
  from have
  group by ID
          ,SVCDATE
  ;
quit;
raajdesaii
Fluorite | Level 6

Thanks! that worked.

mkeintz
PROC Star

Are your data already arranged such that each id/svcdate appears in consecutive records?  If so, then

data have;
  input ID	SVCDATE;
datalines;
1	22016
1	22016
1	22016
1	32016
1	82016
2	72016
2	72016
2	82016
2	102016
run;

data want;
  set have;
  by id svcdate notsorted;
  if last.svcdate;
  count=dif(_n_);
  if count=. then count=_n_;
run;

 

The automatic variable _N_ is the "iteration number" of the data step - in this case equivalent to the observation number.  The DIF function is defined as  DIF(_N_)=_N_-lag(_N_).

 

Since the dif function is only executed at the end of each id/svcdate group, due to the "subsetting IF" statement 

     if last.svcdate;

the lagged value of _N_ will always be the _N_ at the end of the prior group - i.e.   you are calculating

    COUNT= _N_(at end of current group) - _N_(of end of prior group).

 

For the first group COUNT will be missing, so it has to be corrected.

--------------------------
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

--------------------------
PaigeMiller
Diamond | Level 26

@raajdesaii wrote:
This does not get me what I have in the output.

I want to know how many unique date counts are by each id. For e.g. ID1 has 3 counts for 22016, ID1 has 1 count for 32016. I want to do this in a datastep.

It works for me.

 

Doing it in a DATA step is the hard way to do it. You have to write your own code, debug it, and so on, whereas using PROC FREQ,  SAS has already done the hard work of programming it, testing it, and debugging it.

--
Paige Miller
novinosrin
Tourmaline | Level 20

data have;
input ID	SVCDATE;
cards;
1	22016
1	22016
1	22016
1	32016
1	82016
2	72016
2	72016
2	82016
2	102016
;

data want;
 do until(last.svcdate);
  set have;
  by id svcdate notsorted;
  count=sum(count,1);
 end;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3142 views
  • 1 like
  • 5 in conversation