Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

change from character to numeric for a column/variable

Accepted Solution Solved
Reply
Contributor mei
Contributor
Posts: 62
Accepted Solution

change from character to numeric for a column/variable

Dear Sir,

I wish to merge several files with similar variables but certain variables are defined as character in one file but as numeric in another file.

I need to standardize all these variables from character to numeric, ie Yes=1, No/others=0.

How do i do it?

Pls refer to file attached:

Eg: DualClass variable -in character- yes or no need to convert to yes=1, no=0.

other variables are BLANKCHECK CBOARD LSPMT

Pls kindly assist. Thanks a lot.

Attachment

Accepted Solutions
Solution
‎07-10-2017 04:35 PM
Super Contributor
Posts: 1,636

Re: change from character to numeric for a column/variable

try using the code below as an example to write your own code:

data have;

input DualClass $;

cards;

yes

no

YES

No

NO

;

data have(drop=DualClass rename=(_DualClass=DualClass));

  set have;

  if upcase(compress(DualClass))='YES' then _DualClass=1;

  if upcase(compress(DualClass))='NO' then _DualClass=0;

run;

proc print;run;

View solution in original post


All Replies
Contributor mei
Contributor
Posts: 62

Re: change from character to numeric for a column/variable

can i use input function, or should i create a dummy variable first to 1 and 0 say name it as DualClassDummy. Then i will drop the original variable DualClass and rename dualclass dummy as dual class? (so that i can do further merging with the rest of the files?)

Thanks

Solution
‎07-10-2017 04:35 PM
Super Contributor
Posts: 1,636

Re: change from character to numeric for a column/variable

try using the code below as an example to write your own code:

data have;

input DualClass $;

cards;

yes

no

YES

No

NO

;

data have(drop=DualClass rename=(_DualClass=DualClass));

  set have;

  if upcase(compress(DualClass))='YES' then _DualClass=1;

  if upcase(compress(DualClass))='NO' then _DualClass=0;

run;

proc print;run;

Valued Guide
Posts: 765

Re: change from character to numeric for a column/variable

hi ... what do you want is a value is MISSING, another MISSING or consider it a NO (using two of your yes/no variables)

this makes MISSING = MISSING ...


data have;

input (dualclass carveout) (:$3.) ;

datalines;

yes .

no NO

YES .

. YES

NO yes

;


proc format;

invalue yn(upcase) 'YES' = 1 'NO' = 0 other = . ;

run;


data want;

set have;

_dualclass = input(dualclass,yn.);

_carveout  = input(carveout,yn.);

run;

with a lot of yes/no variables, this might save some keystrokes ...

data want;

set have;

array old(*) <list all the y/n variables>;

array new(*) <list all the new variables ... same order as old>;

do _n_=1 to dim(old);

   new(_n_) = input(old(_n_),yn.);

end;

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 987 views
  • 0 likes
  • 3 in conversation