BookmarkSubscribeRSS Feed
ZRick
Obsidian | Level 7

I need to convert some of the following values into numberic when it looks like a numberic, based on its appearances.

How can I develop this logic?

data tmp;

length value $ 4;

input value;

cards;

1234

sys

;;

run;

7 REPLIES 7
Miracle
Barite | Level 11

Hi ZRick. I wonder if this helps.

data tmp;

length value $ 4;

input value;

cards;

1234

sys

;;

run;

data flag; set tmp; flag=ifc(anyalpha(value),'string','numeric'); run;

proc print noobs;; run;

data_null__
Jade | Level 19

K C Wong wrote:

Hi ZRick. I wonder if this helps.

data tmp;

length value $ 4;

input value;

cards;

1234

sys

;;

run;

data flag; set tmp; flag=ifc(anyalpha(value),'string','numeric'); run;

proc print noobs;; run;

You got close but what about E and D  for example "1e6"

data tmp;
   input value $;
   a = input(value,comma8.);
   length flag $8;
   flag=ifc(anyalpha(value),
'string?','numeric?');
  
cards;
1234
-1234
+1234
1.3
1e-3
3d2
4,000
-4,000.9
sys
;;;;
   run;
proc print;
  
run;
Miracle
Barite | Level 11

Unless ZRick's data has such cases ie 1e6  which I dunno about  Smiley Happy

Patrick
Opal | Level 21

You could use something like:

data tmp;

length string $ 4;

input string;

number=input(string,?? best.);

cards;

1234

sys

;;

run;

This will convert the string to a numeric value if possible - else number will contain a missing;

yaswanthj
Calcite | Level 5

Hi..

If you want to create new flags as string and numeric.

data tmp;

length value $ 4;

input value;

cards;

1234

sys

;;

run;

data flag;

set tmp;

string1 = anyalpha(value);

if string1 = 1 then string = value;else number=value; * then string = value;*else value = numeric;

drop string1;

/*put value= flag=;*/

run;

proc print noobs;; run;

Thanks,

Yash

PGStats
Opal | Level 21

Building on DN and Patrick's input :

data tmp;
   length value flag $8;
   input value $&;
   a = input(value,?? comma8.);
   format a best8.;
   flag=ifc(missing(a), 'string', 'numeric');
datalines;
1234
-1234
+1234
1.3
1e-3
3d2
4,000
-4,000.9
sys
;

proc print data=tmp noobs; run;

PG

PG

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 7 replies
  • 1146 views
  • 3 likes
  • 6 in conversation