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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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