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

Hi,  I have a dataset that is interest rate, most of the data is in a 2 decimal format (eg. 2.45), but some rows have a 4 decimals (eg. 2.4678) format.  How do I chose the data that has only the 4 decimals?  Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data have;
 k=2.4678;
 output;
 k=2.34;
 output;
 k=2;
 output;
 k=2.4450;
 output;
 k=2.4400;
run;

data want;
 set have;
 if mod(k*1e4,10);
run;

 /*or where*/

 where mod(k*1e4,10);

View solution in original post

7 REPLIES 7
novinosrin
Tourmaline | Level 20

data have;
 k=2.4678;
 output;
 k=2.34;
 output;
 k=2;
 output;
 k=2.4450;
 output;
 k=2.4400;
run;

data want;
 set have;
 if mod(k*1e4,10);
run;

 /*or where*/

 where mod(k*1e4,10);
PaigeMiller
Diamond | Level 26

The solution from @novinosrin fails if k=2.46789.


How about this:

 

data want;
	set have;
	ychar=put(k,best12.);
	if length(strip(scan(ychar,2,'.')))=4;
run;

 And @Tom makes a good point too... 

--
Paige Miller
novinosrin
Tourmaline | Level 20

Yes, that's correct. The solution assumes based upon OP wrote "most of the data is in a 2 decimal format (eg. 2.45), but some rows have a 4 decimals (eg. 2.4678) format."

podarum
Quartz | Level 8
Thanks everyone, the answers are great
Tom
Super User Tom
Super User

@podarum wrote:

Hi,  I have a dataset that is interest rate, most of the data is in a 2 decimal format (eg. 2.45), but some rows have a 4 decimals (eg. 2.4678) format.  How do I chose the data that has only the 4 decimals?  Thanks


If the field is not a character string then you don't.

How can you tell the difference between a value that was original written as '2.50' and a value written as '2.500'?  They are the same number. 

Reeza
Super User
What's your original data source? Can you read it in as character first?
This would solve your problem, technically....you may still have logic issues.
ChrisNZ
Tourmaline | Level 20

Another way:

 if length( scan( put(VAR,best32. -l), 2, '. ' ) ) = 4;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1399 views
  • 0 likes
  • 6 in conversation