Discover More Tips and Techniques on This Blog

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;

4 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

Disclosure:

In the spirit of transparency and innovation, I want to share that some of the content on this blog is generated with the assistance of ChatGPT, an AI language model developed by OpenAI. While I use this tool to help brainstorm ideas and draft content, every post is carefully reviewed, edited, and personalized by me to ensure it aligns with my voice, values, and the needs of my readers. My goal is to provide you with accurate, valuable, and engaging content, and I believe that using AI as a creative aid helps achieve that. If you have any questions or feedback about this approach, feel free to reach out. Your trust and satisfaction are my top priorities.