BookmarkSubscribeRSS Feed
sasuser123123
Quartz | Level 8

Hi Everyone.
I have data like...

 

data nn;
input id res $30.;
cards;
101 33
102 35
103 22
104 <23
105 <55
105 Above 33.3
106 normal
107 abnormal,1.1
108 active,2.3
109 Above 1
110 <44
;

 

I need output like....

ID    res                     Num    Char
101  33                      33        
102  35                      35        
103  22                      22       
104  <23                                 <23
105 <55                                  <55
105  Above 33.3                     Above 33.3
106  normal                             normal
107  abnormal,1.1    1.1         abnormal
108  active,2.3          2.3         active
109  Above 1                           Above 1
110  <44                                  <44


I have used compress function..

num=compress(res,"0123456789.","K");
char= compress(res,"0123456789,");

But Im getting wrong result for few records like ID's 105,106,109,110
for example <23 we should keep in char not in num
and for 105 and 110 records we should keep it like as it is in char example.."Above 33.3"
And if we have ',' in result we can separate it as like 107 and 108 in output

 

 

Could anyone please assist me for how to do this task..it would be great help for me

Thanks

3 REPLIES 3
ballardw
Super User

@sasuser123123 wrote:

Hi Everyone.
I have data like...

 

data nn;
input id res $30.;
cards;
101 33
102 35
103 22
104 <23
105 <55
105 Above 33.3
106 normal
107 abnormal,1.1
108 active,2.3
109 Above 1
110 <44
;

 

I need output like....

ID    res                     Num    Char
101  33                      33        
102  35                      35        
103  22                      22       
104  <23                                 <23
105 <55                                  <55
105  Above 33.3                     Above 33.3
106  normal                             normal
107  abnormal,1.1    1.1         abnormal
108  active,2.3          2.3         active
109  Above 1                           Above 1
110  <44                                  <44


I have used compress function..

num=compress(res,"0123456789.","K");
char= compress(res,"0123456789,");

But Im getting wrong result for few records like ID's 105,106,109,110
for example <23 we should keep in char not in num
and for 105 and 110 records we should keep it like as it is in char example.."Above 33.3"
And if we have ',' in result we can separate it as like 107 and 108 in output

 

 

Could anyone please assist me for how to do this task..it would be great help for me

Thanks


One way:

data want;
   set nn;
   num= input(res,?? f6.);
   if missing(num) then char=res;
run;

Input with an appropriate informat is the proper way to read character values into numeric. The ?? suppresses the invalid data messages that would normally apply because the character variable Res contains non-numeric information.

Then assign the character version when that conversion to numeric fails.

I leave the res variable in the data. Use Drop if you don't want it.

sasuser123123
Quartz | Level 8

Thank you for your response

 

Yeah its partially working ...for 107 and  108 we have num and char separated with ','

when Im using your code those values going to char like "abnormal,1.1". I need Abnormal in char and 1.1 in num

ballardw
Super User

@sasuser123123 wrote:

Thank you for your response

 

Yeah its partially working ...for 107 and  108 we have num and char separated with ','

when Im using your code those values going to char like "abnormal,1.1". I need Abnormal in char and 1.1 in num


Are those two the only sort of really bad data entry or do you have more, such as have colon, semicolon or other separators? The numeric values coming before the text? Between text?

Look at SCAN.

Try to convert each piece to numeric. If one is successful then maybe the rest is the text you want.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 847 views
  • 2 likes
  • 2 in conversation