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

Create a dataset to genrate the count of the number of student in a STUD dataset and also display records only if student count is 3 or more than 3

STUD Dataset :

STUDID         MARKS
1                     24
1                     24
1                     24
2                     24
2                     24
3                     24

O/P Requirement :

STUDID MARKS NEWVAR (U NEED TO CREATE)
1               24              1
1               24              2
1               24              3
2               24              1
2               24              2
3               24              1

 

can i get the output using proc freq procedure? (or) Which mehod is good to go?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Determine the number of obs per studid in an intermediate step and then merge with the original dataset.

Note the use of the keep= option.

data have;
input studid marks;
cards;
1 24
1 24
1 24
2 24
2 24
3 24
;
run;

data int (keep=studid);
set have;
by studid;
if first.studid
then newvar = 1;
else newvar + 1;
if last.studid and newvar > 1;
run;

data want;
merge
  have
  int (in=a)
;
by studid;
if a and (first.studid or last.studid);
run;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

Just use by processing and the correct syntax so newvar is automatically retained:

data want;
set have;
by studid;
if first.studid
then newvar = 1;
else newvar + 1; * this simple increment statement makes newvar a retained variable;
run;

 

shahnaz
Calcite | Level 5

Ohh Thank u soo much KurtBremser for the quick reply. And I have 1 more question for the same dataset:

 

Consider above student dataset and create a new dataset with only FIRST and LAST record of a student. If count =1 then no need to populate that record

 

Regards,

Shahnaz

Kurt_Bremser
Super User

Determine the number of obs per studid in an intermediate step and then merge with the original dataset.

Note the use of the keep= option.

data have;
input studid marks;
cards;
1 24
1 24
1 24
2 24
2 24
3 24
;
run;

data int (keep=studid);
set have;
by studid;
if first.studid
then newvar = 1;
else newvar + 1;
if last.studid and newvar > 1;
run;

data want;
merge
  have
  int (in=a)
;
by studid;
if a and (first.studid or last.studid);
run;

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
  • 3 replies
  • 805 views
  • 1 like
  • 2 in conversation