Hello,
I want to separate out two variables from one.
Have
Obs REPVAL
1 2+
2 90.0
3 -
4 34
5 +
6 <9
Want
obs REPVAL CHVAL NUMVAL
1 2+ 2+
2 90.0 90.0
3 - -
4 34 34
5 + +
6 <9 <9
Question is I have dataset want and need output have.
Character variables in one columns and Numeric columns in other one.
Please assume Character values can be anything +2,4+,-,+,<00.89 where as numeric values has to integers such as 28.90,39, 80.0
dictionary :
1) REPVAL: reported values (Have)
2) CHVAL: Character values (Want)
3) NUMVAL: Numeric Values (Want)
Please do needful help.
Anything that doesn't read as a number is considered character:
data have;
input repval $;
datalines;
2+
90.0
-
34
+
<9
;
data want;
set have;
numval = input (repval, ?? best.);
if missing(numval) then chval = repval;
run;
EDIT : same as @Reeza, She's quicker.
Integers or decimals?
One option - try and read it as a numeric, if it doesn't read it will stay as missing. For character variable, check if the numeric is still missing and then assign it to the character variable otherwise.
data want;
set have;
length numval 8. chval $20.;
numval = input(repval, ?? best.);
if missing (numval) then chval = repval;
run;
Some other options:
Use NOTDIGIT or NOTALPHA and/or ANYDIGIT family of functions to determine if there's a character in there or not. The period/decimal point will be slightly problematic in this approach though.
EDIT: typo with the variable name num_val should be numval.
@shahparth260 wrote:
Hello,
I want to separate out two variables from one.
Have
Obs REPVAL
1 2+
2 90.0
3 -
4 34
5 +
6 <9
Want
obs REPVAL CHVAL NUMVAL
1 2+ 2+
2 90.0 90.0
3 - -
4 34 34
5 + +
6 <9 <9
Question is I have dataset want and need output have.Character variables in one columns and Numeric columns in other one.
Please assume Character values can be anything +2,4+,-,+,<00.89 where as numeric values has to integers such as 28.90,39, 80.0
dictionary :
1) REPVAL: reported values (Have)
2) CHVAL: Character values (Want)
3) NUMVAL: Numeric Values (Want)
Please do needful help.
Assuming the check is punctuation chars besides '.' since your sample isn't extensive
data have;
input Obs REPVAL $;
cards;
1 2+
2 90.0
3 -
4 34
5 +
6 <9
;
data want;
set have;
if anypunct(compress(REPVAL,'.'))>0 then char=repval;
else numv=repval;
run;
Thank you so much for your contribution.
Anything that doesn't read as a number is considered character:
data have;
input repval $;
datalines;
2+
90.0
-
34
+
<9
;
data want;
set have;
numval = input (repval, ?? best.);
if missing(numval) then chval = repval;
run;
EDIT : same as @Reeza, She's quicker.
@ Thank you so much for your kind help.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.