How to remove special characters from a dataset in SAS?

sas
data_wrangling

#1

Let us say I have input data set as:

data s;
input id$;
cards;
8.8gm
32$
45<
9.8%
;
run;

Output dataset should have only numerical part, but no special characters. Like, in the case below, the output should be:
8.8
32
45
9.8

i hope modifier in compress function does’nt work on this. there must be some other technique.


#2

@Armaan

I have used modifier in compress function and it has solved the problem.

data d;
input id$;
cards; 
8.8gm
32$
45<
9.8%
;
run;

Data a;
set d;
c=compress(id,'','kd');
run;

Output

List of Modifiers:

a – Compress or Delete all upper and lower case characters from String.

ak- Compress or Delete alphabetic characters(1,2,3 etc) from String.

kd- Compress or Delete characters(aplabets) from String.( Keeps only digits).

d – Compress or Delete numerical values from String.

i – Compress or Delete specified characters both upper and lower case from String.

k – keeps the specified characters in the string instead of removing them.

l – Compress or Delete lowercase characters from String.

p – Compress or Delete Punctuation characters from String.

s – Compress or delete spaces from String. This is default.

u – Compress or Delete uppercase characters from String.

Hope this helps!

Regards,
Sunil


#3

but sir my first observation value shud be 8.8 and last obs value shud be 9.8, i hav tried dis technique with modifiers in compress but didn’t worked on the floating values. my main motto is to hav the floating values to be same as 8.8 and 9.8 and the rest values be only digits.
any ways thank u @sunil sir… :slight_smile:


#4

@Armaan

You can use below code to have floating values.

Data a;
set d;
c=compress(id,'0123456789.','k');
run;

Output:

Regards,
Sunil


#5

Wow !!!
That’s Miracle Sunil sir…
thank you soo muchhh…i’ve never thought 2nd argument in the compress funxn can be used like this with the modifier, as you did.
thank you sir :smile: