BookmarkSubscribeRSS Feed
pritam26
Fluorite | Level 6

 

Given Dataset: All hav Char type, i need to change the date type to numeric with odinal value.

Gender

MarriedDependentsEducation
MaleYes0Graduate
MaleYes1Graduate
MaleYes2Graduate
MaleYes2Graduate
MaleNo0Not Graduate
MaleYes0Not Graduate

 

Used Proc format  to change the format:
proc format;
value $G
'Male'=1 'Female'=0;
Value $M
'Yes'=1 'No'=0;
value $D
"0"=0 "1"=1 "2"=2 "3+"=3;
value $E
"Graduate"=1 "Not Graduate"=0 ;

run;

 

BUT NOW HOW I CAN CHANGE THE TYPE FROM CHAR TO NUMERIC??

 

 

 

4 REPLIES 4
Daniel-Santos
Obsidian | Level 7

Hi. 

 

It actually works if you explicitly define the needed vars as numeric.

 

You can use the LENGTH statement for this.

 

data _OUT1;
     length GENDERN 8.;
     GENDERN=put('Male',$G.);
run;

 

Off course this will produce a warning, as SAS is implictly doing the type conversion for you.

 

Another way of doing this, and surely the right way would be to create numeric informats instead of formats.

 

For example:

 

proc format;
     invalue G
           'Male'=1 'Female'=0;
     invalue M
           'Yes'=1 'No'=0;
     invalue D
           "0"=0 "1"=1 "2"=2 "3+"=3;
     invalue E
           "Graduate"=1 "Not Graduate"=0 ;

run;

 

data _OUT2;
     GENDERN=input('Male',G.);
run;

 

Hope it helps.

 

Daniel Santos @ www.cgd.pt

Patrick
Opal | Level 21

Not sure why you have to recode your variables this way but to answer your question: Use a numeric INformat as this will allow you to read a string and translate it into a numeric value.

proc format;
  invalue sex_num
    "Male" =1
    "Female"=2
    other=99
   ;
quit;

data sample;
  sex="Female";
  sex2=input(sex,sex_num.);
run;

 

pritam26
Fluorite | Level 6
Need to run regression model. Thanks for the help
Patrick
Opal | Level 21

If your source data is not already standardized then also use the UPCASE option so that the informat is no more case sensitive.

proc format;
  invalue sex_num (just upcase)
    "MALE"  =1
    "FEMALE"=2
    other=99
    ;
quit;

data sample;
  sex="female";
  sex2=input(sex,sex_num.);
run;

UPCASE

converts all raw data values to uppercase before they are compared to the possible ranges. If you use UPCASE, then make sure the values or ranges that you specify are in uppercase.

http://support.sas.com/documentation/cdl/en/proc/69850/HTML/default/viewer.htm#p1pmw90bl3jzgdn1w4202...

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 11547 views
  • 4 likes
  • 3 in conversation