BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ranny
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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

6 REPLIES 6
Jagadishkatam
Amethyst | Level 16

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
Linlin
Lapis Lazuli | Level 10

or you can use MDY function:

data want;

    set have;

    date=mdy(month,day,year);

    format date mmddyy10.;

run;

Cynthia_sas
SAS Super FREQ

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

Ranny
Obsidian | Level 7

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

TomKari
Onyx | Level 15

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

art297
Opal | Level 21

: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=m:)/view=vwant;

  format month_of_observation $7.;

  set have;

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

run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 20438 views
  • 13 likes
  • 6 in conversation