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

I have a variable which has values like >=500, >75 in addition to truly numeric values in integers.

I need to convert it to a numeric variable while ignoring the =, <= etc.

The SAS input function does not work in this case.

Thank you.

Here is a sample set

data what;

input  $lab;

datalines;

<=500

<75

=<100

2000

300

400

10

;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
hsharmas
Fluorite | Level 6

Hi, you can use a compress function here in ways it fits best and if the variable is non numeric use the input function to convert it to numeric.

Example: kk = compress(lab,"<= ");  /*Here it will compress <= and blanks in the variable*/

           OR

                kk = compress(lab,"1234567890","k")   /* This will keep only 1234567890 values in the variable and it you have a decimal you can include . in the list*/

Use of input function: jj = input(kk,best12.);     /***Assigning informat to the variable******/

                                      format jj best12.;    /*****Output/Display format***************/

Hope that helps!

Regards,

Somi

View solution in original post

3 REPLIES 3
hsharmas
Fluorite | Level 6

Hi, you can use a compress function here in ways it fits best and if the variable is non numeric use the input function to convert it to numeric.

Example: kk = compress(lab,"<= ");  /*Here it will compress <= and blanks in the variable*/

           OR

                kk = compress(lab,"1234567890","k")   /* This will keep only 1234567890 values in the variable and it you have a decimal you can include . in the list*/

Use of input function: jj = input(kk,best12.);     /***Assigning informat to the variable******/

                                      format jj best12.;    /*****Output/Display format***************/

Hope that helps!

Regards,

Somi

saurabhc
Fluorite | Level 6

I tried substr function. It is a longer way of doing this job.

if substr(result, 1, 2) in("<=","=<",">=")then results=substr(result,3,10);

else if substr(result,1,1)="<" then results=substr(result, 2,10);

else results=result;

results1=input(results,best12.);

Your code totally works though.

Thanks!

hsharmas
Fluorite | Level 6

K is a modifier and is used for keep. Also, in your method you could do it when obs are less as you can look and check where is the position of <>=, and impossible when their are thousands of obs.

Use this link:

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

Rate me please if I answered you query.

Regards,

Somi

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