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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 2784 views
  • 0 likes
  • 3 in conversation