BookmarkSubscribeRSS Feed
xyxu
Quartz | Level 8

I have a $20 char variable that can take the following values:

 

data have;

    input var $20.;

    cards;

0.02

0.0125

0.05%

3

0.0100

0.5ABC

0.02or>5

run;

 

I want to keep only observations with purely numerical values: 0.02, 0.0125, 0.05%, 3, 0.0100. I also want these values to be formatted. How can I do this? 

3 REPLIES 3
hashman
Ammonite | Level 13

@xyxu:

From looking at your sample data, it appears that it can be as simple as using the COMMAw. informat:

 

data have ;                         
  input var $20. ;                  
  cards ;                           
0.02                                
0.0125                              
0.05%                               
3                                   
0.0100                              
0.5ABC                              
0.02or>5                            
run ;                               
                                    
data want ;                         
  set have ;                        
  value = input (var, ?? comma20.) ;
  if N (value) ;                    
run ;                               

However, be aware that COMMAw. merely strips the % sign (and also $ and some other characters) - it doesn't divide the result by 100. If that is what you want, you'd have to add more logic to handle it. Also, you aren't telling how you want to format the resulting variable, so I can't offer an advice on that head.

 

Paul D. 

 

 

PGStats
Opal | Level 21

Use informat percent20. instead. It also removes dollar signs, negates values in parentheses and divides by 100 only if a percent sign is present.

PG
hashman
Ammonite | Level 13

@PGStats:

 

An excellent suggestion if this is indeed what the OP wants.

 

Paul D.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 3 replies
  • 1108 views
  • 4 likes
  • 3 in conversation