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

I'd like to create a var "count" for the total number of repated rows in combination of multiple variables as shown in data "want". I know how to accomplish this using proc freq and merging output data back to original file. I wonder if i can do it in data step, as shown how my program had failed to do so below. I'm using SAS 9.4.

Could you please help with direct correction on my code below?

data have;
input id cat dog ant;
datalines;
1 1 2 3
1 1 2 3
1 1 2 3
1 1 2 3
2 2 6 0
3 3 4 5
4 4 6 7
;
run; 

data want;
input id cat dog ant count;
datalines;
1 1 2 3 4
1 1 2 3 4
1 1 2 3 4
1 1 2 3 4
2 2 6 0 1
3 3 4 5 1
4 4 6 7 1
;
run; 

PROC SORT DATA=have;
BY id;
RUN;
DATA HAVE1; SET HAVE;
RETAIN COUNT;
BY ID;
IF FIRST.ID THEN VISIT = 1; ELSE VISIT+1;
IF LAST.ID THEN COUNT=VISIT;
RUN;

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21
PROC SORT DATA=have;
  BY id;
RUN;

DATA HAVE1;
  do until(last.id);
    SET HAVE;
    BY ID;
    if first.id then count=1;
    else count+1;
  end;
  do until(last.id);
    SET HAVE;
    BY ID;
    output;
  end;
RUN;

Art, CEO, AnalystFinder.com

 

View solution in original post

2 REPLIES 2
art297
Opal | Level 21
PROC SORT DATA=have;
  BY id;
RUN;

DATA HAVE1;
  do until(last.id);
    SET HAVE;
    BY ID;
    if first.id then count=1;
    else count+1;
  end;
  do until(last.id);
    SET HAVE;
    BY ID;
    output;
  end;
RUN;

Art, CEO, AnalystFinder.com

 

Cruise
Ammonite | Level 13
Баярлалаа. Thank you.

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!

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