BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AKHILA
Obsidian | Level 7
I have a dataset like
Id name
1 negative
2 45
3 1E
4 positive
5 24

I want to derive a new variable 'Flag' in which the values having alphabets should be ' '(missing character value) and no change for others.like,
1
2 45
3
4
5 24

Someone pls help..
Thanks
1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

 

data want;
  set have;
  if lengthn(comrpess(name,".","d"))=0 then name_derived=compress(name,".","kd");
run;

Add the dot in.  Is it that you want a numeric value?  If so:

data have;
  a="123"; output;
  a="1.45"; output;
  a="XYZ"; output;
run;

data want;
  set have;
  if lengthn(compress(a,".","d"))=0 then b=input(a,best.);
run;

 

View solution in original post

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Something like:

data want;
  set have;
  if lengthn(comrpess(name," ","d"))=0 then name_derived=compress(name," ","kd");
run;

Not tested as you have not provided test data in the form of a datastep (and not here to type in test data!).

What this does is check the length when numbers are removed, if there is nothing but numbers, then keep only them.  Compress is described in the docs:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212246.htm

AKHILA
Obsidian | Level 7
Its working what to do if i want decimal digits also like 4.2 , 1.8 etc
RW9
Diamond | Level 26 RW9
Diamond | Level 26

 

data want;
  set have;
  if lengthn(comrpess(name,".","d"))=0 then name_derived=compress(name,".","kd");
run;

Add the dot in.  Is it that you want a numeric value?  If so:

data have;
  a="123"; output;
  a="1.45"; output;
  a="XYZ"; output;
run;

data want;
  set have;
  if lengthn(compress(a,".","d"))=0 then b=input(a,best.);
run;

 

Astounding
PROC Star
What should happen when NAME contains blanks such as "7 11"

I'm guessing that should be removed. In that case, you can try:

if input( name, ?? 32.) > . then flag = name;
Sathish_jammy
Lapis Lazuli | Level 10

The give code will drop alphabets and spaces, only contains numeric(float datatype as well)

 

data want(drop = YourVariable);

set have;

NewVariable = input(compress(YourVariable,'1234567890.','k'),best12.);

run;

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Unfortunately that doesn't match the required output (otherwise it would have been mentioned):

data have;
  input yourvariable $;
datalines;
negative
45
1E
positive
24
;
run;


data want (drop=yourvariable);
  set have; 
  newvariable=input(compress(yourvariable,'1234567890.','k'),best12.);
run;

See row 3.

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
  • 7 replies
  • 2343 views
  • 1 like
  • 5 in conversation