Labels

Thursday, August 12, 2010

SAS character to number or date

Character to number:: input( charvar, 6.0);
number to Character:: put(numvar, 4.0);
character to date:: input(charvar, mmddyy10.);
number to date:: mdy(mon, date, year);
remove specific character from a string:: compress(charvar, '()- '); *remove the (, ), -, and white space from the variable.



/* Program 1:: Convert num to char and char to num */

data temp1;
input numvar charvar $4.;

/* convert character to numeric */
numvar_n=input(charvar, 4.);

/* convert numeric to character */
char_n=put(numvar,4.0);

cards;
123 7691
789 0306
011 8729
;;

proc print data=temp1;
run;



/*program 2 character to date and number to date*/

data temp2;
input date_char mon day year;

/* convert character to date */
date1=input(date_char, mmddyy10.);

/* convert numeric to date */
date2=mdy(mon,day,year);

cards;
10/01/1999 10 01 1999
12/24/2003 12 24 2003
07/01/2001 7 1 2001
;;
proc print data=temp2;
run;

*program 3 remove the white space from a date variable;

data temp2;
input date_char mon day year;

/* convert character to date */
date1=input(compress(date_char), mmddyy10.);
*here the compress without the second parameter;
*by default it is the white space;

/* convert numeric to date */
date2=mdy(mon,day,year);

cards;
*instead of 10/01/1999, this format is 10/ 1/1999 using a space instead of 0;
10/ 1/1999 10 01 1999
12/24/2003 12 24 2003
7/ 1/2001 7 1 2001
;;
proc print data=temp2;
run;

3 comments:

  1. Helpful post. Thanks for sharing this program, continue sharing more like this.
    SAS Training in Chennai | SAS Course in Chennai

    ReplyDelete
  2. Totally concur with you Gil! Despite the fact that a considerable measure of things proceed onward to the advanced world, does not mean conventional showcasing isn't as compelling! dotnet training in bangalore
    Dotnet developer

    ReplyDelete
  3. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.

    Best Java Training Institute Chennai


    Amazon Web Services Training in Chennai


    ReplyDelete