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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 965 views
  • 0 likes
  • 6 in conversation