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.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.