DATA Step, Macro, Functions and more

create 1 variable form 3 variables - date

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

create 1 variable form 3 variables - date

Hi,

I have input file look likes .... (have 3 variables)

Month             day                     year

11                    30                      2011

11                    03                      2012

11                    29                      2012

12                    20                      2010

I want output as a .. (1 variable only, so I have to combine above 3 variables into 1 variable)


Date

11/30/2011

11/03/2012

11/29/2012

12/20/2010

Can you please provide me sample code for this ??

Thank you.

Ranny


Accepted Solutions
Solution
‎12-13-2012 04:37 AM
Trusted Advisor
Posts: 1,128

Re: create 1 variable form 3 variables - date

Hi Ranny,

You can use the catx function to get the output as below

data have;

    input month day year;

cards;

11                    30                      2011

11                    03                      2012

11                    29                      2012

12                    20                      2010

;

run;

data want;

    set have;

    date=input(catx("/",month,day,year),mmddyy10.);

    format date mmddyy10.;

run;

The catx function will concatenate the three variables with a separator and input convert the character date to numeric date and a mmddyy10. format is used to write the date.

Thanks,

Jag

Thanks,
Jag

View solution in original post


All Replies
Solution
‎12-13-2012 04:37 AM
Trusted Advisor
Posts: 1,128

Re: create 1 variable form 3 variables - date

Hi Ranny,

You can use the catx function to get the output as below

data have;

    input month day year;

cards;

11                    30                      2011

11                    03                      2012

11                    29                      2012

12                    20                      2010

;

run;

data want;

    set have;

    date=input(catx("/",month,day,year),mmddyy10.);

    format date mmddyy10.;

run;

The catx function will concatenate the three variables with a separator and input convert the character date to numeric date and a mmddyy10. format is used to write the date.

Thanks,

Jag

Thanks,
Jag
Super Contributor
Posts: 1,636

Re: create 1 variable form 3 variables - date

or you can use MDY function:

data want;

    set have;

    date=mdy(month,day,year);

    format date mmddyy10.;

run;

SAS Super FREQ
Posts: 8,739

Re: create 1 variable form 3 variables - date

Hi:

  And,  Linlin's solution (to create a numeric variable for date) would allow you to use the new version of the date variable to calculate things like duration and age or length of time. But, if you just create a character string using concatenate functions, then you are stuck with a character string and you can't calculate anything with a character string.

  SAS stores dates, internally in storage, as the number of days from January 1, 1960. So that these are the internally stored values for the following dates:

Jan  1, 1960        0

Dec 31, 1959       -1

Dec  1, 1950    -3318

Jan  1, 1582  -138061

Dec 13, 2012    19340

  If your date is stored internally as the number of days either before or after Jan 1, 1960, then you can always display that number in a meaninful/readable form, by using one of the SAS date formats, such as the mmddyy10. format shown in Linlin's program.

  The beauty of the date formats is that you have great flexibility in how you display the date value, so the internal number '0' could be displayed in a number of different ways:

  Format    internal  Display AS

  mmddyy10      0     01/01/1960

  mmddyy8       0     01/01/60

  worddate      0     January 1, 1960

  weekdate      0     Friday, Jan 1, 1960

  date7         0     01JAN60

  date9         0     01JAN1960

  ddmmyy6       0     010160

  ddmmyy10      0     01/01/1960

  year4         0     1960

  My .02, in the long run, it would be better for your date variables to be stored correctly as numbers and not character strings.

cynthia

Occasional Contributor
Posts: 19

Re: create 1 variable form 3 variables - date

Thank you to Jagdish, Linlin and Cynthia for provide me three different direction for same Job !!

Cynthia, Your post are really helpful, I am learning SAS and sometimes I gets answer from your old post and thank you for giving a good description, We really need as a SAS Lerner !!

Regards,

Ranny

PROC Star
Posts: 1,086

Re: create 1 variable form 3 variables - date

Hi, Cynthia

Your response triggers something I've been wondering about for a while.

Sometimes I need to do something by month or by year. I like using date variables for these, for all of the reasons you describe.

Can you suggest a best practice for what to do about the date parts that I don't need? For example, if I want a table that looks like:

Month of ObservationMean
1988-01722.2059995
1988-02740.3119994
1988-03766.4852169
1988-04759.0969995
1992-021164.90

I don't need a value for day. Any suggestions for what I should use?

Tom

PROC Star
Posts: 7,356

Re: create 1 variable form 3 variables - date

:It will be interesting to see what everyone thinks would be among the best practices for what you want.  My own preference would be to build what you want into a view.  e.g.:

data have;

  input date date9. mean;

  cards;

05jan1988 722.2059995

02feb1988 740.3119994

01mar1988 766.4852169

05apr1988 759.0969995

01feb1992 1164.90

;

data vwant (keep=mSmiley Happy/view=vwant;

  format month_of_observation $7.;

  set have;

  month_of_observation=substr(put(date,yymmdd10.),1,7);

run;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 7558 views
  • 12 likes
  • 6 in conversation