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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

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

3 REPLIES 3
mei
Calcite | Level 5 mei
Calcite | Level 5

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

Linlin
Lapis Lazuli | Level 10

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;

MikeZdeb
Rhodochrosite | Level 12

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;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to choose a machine learning algorithm

Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.

Find more tutorials on the SAS Users YouTube channel.

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