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

Dear All,

 

Could you please help me with the following problem? I have dataset that looks like this:

 

Company        Date         Var1          

   1                     1             20           

   1                     2             20             

   1                     3             20             

   1                     4             20             

   1                     5             20             

   1                     6             35             

   1                     7             35               

   1                     8             35             

   1                     9             28             

   1                     10           28            

   1                     11           28             

   1                     12           56             

   1                     13           56             

   1                     14           56                  

   2                     1             20             

   2                     2             20             

   2                     3             20             

   2                     4             18             

   2                     5             18             

   2                     6             18             

   2                     7             27      

   3                     4              5

   3                     5              5

   3                     6              5

   3                     7              5

   3                     8              5

   3                     9              5

   3                   10              9

   3                   11              9

   3                   12              9

 

I need to obtain the following dataset:

 

Company        Date         Var1          

   1                     1             0           

   1                     2             0             

   1                     3             20             

   1                     4             20             

   1                     5             20             

   1                     6             35             

   1                     7             35               

   1                     8             35             

   1                     9             28             

   1                     10           28            

   1                     11           28             

   1                     12           56             

   1                     13           56             

   1                     14           56                  

   2                     1             20             

   2                     2             20             

   2                     3             20             

   2                     4             18             

   2                     5             18             

   2                     6             18             

   2                     7             27      

   3                     4              0

   3                     5              0

   3                     6              0

   3                     7              5

   3                     8              5

   3                     9              5

   3                   10              9

   3                   11              9

   3                   12              9

 

In other words, for each company I need to replace with zero those values that are repeated more than three times. I want to leave these three repeated values and remaining repeated values replace with zero. I also think that values that are repeated more than three times are usually placed at the beginning of each company.

 

Any help would be hugely appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input Company        Date         Var1   ;
cards;       
   1                     1             20           
   1                     2             20             
   1                     3             20             
   1                     4             20             
   1                     5             20             
   1                     6             35             
   1                     7             35               
   1                     8             35             
   1                     9             28             
   1                     10           28            
   1                     11           28             
   1                     12           56             
   1                     13           56             
   1                     14           56                  
   2                     1             20             
   2                     2             20             
   2                     3             20             
   2                     4             18             
   2                     5             18             
   2                     6             18             
   2                     7             27      
   3                     4              5
   3                     5              5
   3                     6              5
   3                     7              5
   3                     8              5
   3                     9              5
   3                   10              9
   3                   11              9
   3                   12              9
;
run;
data want;
n=0;
 do until(last.Var1);
 set have;
 by Company  Var1 notsorted;
 n+1;
 end;
m=0 ;
 do until(last.Var1);
 set have;
 by Company  Var1 notsorted;
 m+1;
 if n gt 3 and m le (n-3) then do;Var1=0;output;end; 
  else output;
 end;
drop m n;
run;

View solution in original post

3 REPLIES 3
Reeza
Super User

Does it matter if it's the latest or earliest that are replaced?

 

proc sort data=have;
by company descending date var1;
run;

data want;
set have;
by company descending date;
if first.company then count=0;

if first.var1 then count=1;
else count+1;

if count>3 then var1=0;

run;

proc sort data=want;
by company date;
run;
Ksharp
Super User
data have;
input Company        Date         Var1   ;
cards;       
   1                     1             20           
   1                     2             20             
   1                     3             20             
   1                     4             20             
   1                     5             20             
   1                     6             35             
   1                     7             35               
   1                     8             35             
   1                     9             28             
   1                     10           28            
   1                     11           28             
   1                     12           56             
   1                     13           56             
   1                     14           56                  
   2                     1             20             
   2                     2             20             
   2                     3             20             
   2                     4             18             
   2                     5             18             
   2                     6             18             
   2                     7             27      
   3                     4              5
   3                     5              5
   3                     6              5
   3                     7              5
   3                     8              5
   3                     9              5
   3                   10              9
   3                   11              9
   3                   12              9
;
run;
data want;
n=0;
 do until(last.Var1);
 set have;
 by Company  Var1 notsorted;
 n+1;
 end;
m=0 ;
 do until(last.Var1);
 set have;
 by Company  Var1 notsorted;
 m+1;
 if n gt 3 and m le (n-3) then do;Var1=0;output;end; 
  else output;
 end;
drop m n;
run;
Haikuo
Onyx | Level 15

Could be as simple as:

data want;
	do _n_=1 by 1 until (last.var1);
		set have;
		by company var1 notsorted;

		if _n_>3 then
			var1=0;
		output;
	end;
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
  • 669 views
  • 1 like
  • 4 in conversation