BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
shahparth260
Quartz | Level 8

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.

 

 

 

PS
1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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. 

PG

View solution in original post

5 REPLIES 5
Reeza
Super User

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.

 

 

 


 

novinosrin
Tourmaline | Level 20

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;
shahparth260
Quartz | Level 8

Thank you so much for your contribution.

PS
PGStats
Opal | Level 21

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. 

PG
shahparth260
Quartz | Level 8

@ Thank you so much for your kind help. 

PS

sas-innovate-wordmark-2025-midnight.png

Register Today!

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.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1093 views
  • 2 likes
  • 4 in conversation