Convert values from character to numeric or from numeric to character.

Convert values from character to numeric or from numeric to character\Convert variable values using either the INPUT or PUT function.
Convert a character value to a numeric value by using the INPUT function. Specify a numeric informat that best describes how to Read the data value into the numeric variable. When changing types a new variable name is required. If you need to keep the original variable name for the new type, use the RENAME= option as illustrated in Sample 2.


data char;
input string :$8. date :$6.;
numeric=input(string,8.);
sasdate=input(date,mmddyy6.);
format sasdate mmddyy10.;
datalines;
1234.56 031704
3920 123104;


proc print;
run;








data now_num;
input num date: mmddyy6.;
datalines;
123456 110204
1000 120504;
 

run;




data now_char;
set now_num (rename=(num=oldnum date=olddate));
num=put(oldnum,6. -L);
date=put(olddate,date9.);
run;

proc print;
run;



Source: support.sas.com


Here is how to convert Numeric date with  yymmdd8. format to mmddyy10. format.

data datec;

input numdate;
date =input(put(numdate,z8.),yymmdd8.);
format date mmddyy10.;
datalines;
20040625
20081219
;
run;

Comments

  1. Thanks, I was just trying to figure this out, and came across your blog. It was just what I was looking for.

    ReplyDelete
  2. My problem is the data in numerical, this is wind in degrees of direction. I have to replace values with the directions in names: N, NNE, ENE and CALM for 0 degree direction.
    I tried several ways, the output comes as missing values or/and creates variables with the names of directions.... Could you help me please.....

    ReplyDelete
  3. I could help if you can share your data?

    Sarath

    ReplyDelete
  4. my input is:-
    1 2 3 4 5 6
    1 2 3 4 5 6
    1 2 3 4 5 6
    1 2 3 4 5 6
    1 2 3 4 5 6
    1 2 3 4 5 6

    After SAS operation
    Output should be :
    1 2 3 4 5 6
    2 3 4 5 6
    3 4 5 6
    5 6
    6

    ReplyDelete

Post a Comment

Popular posts from this blog

SAS Interview Questions and Answers: CDISC, SDTM and ADAM etc

Comparing Two Methods for Removing Formats and Informats in SAS: DATA Step vs. PROC DATASETS

Studyday calculation ( --DY Variable in SDTM)