Help using Base SAS procedures

Split Dataset by Year from mm/dd/yyyy Format

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

Split Dataset by Year from mm/dd/yyyy Format

I have a dataset ("Dataset1") with a date column ("Date").  The format is mm/dd/yyyy.

I want to make multiple data files with the information from the rows split up by year.

So it would go from Dataset1 to datasets "1999" and "1998" etc.

Can someone help me with the code?


Accepted Solutions
Solution
‎04-08-2014 07:55 PM
Super User
Posts: 17,912

Re: Split Dataset by Year from mm/dd/yyyy Format

Why do you want to split up your datasets? This is a commonly asked question, the answer is usually don't do it. 

The manual example, that assumes that your date variable is a SAS numeric variable with a date format, not a character variable:

data Y1998 Y1999;

set have;

if year(date) = 1998 then output Y1998;

else if year(date) = 1999 then output Y1999;

run;

View solution in original post


All Replies
Solution
‎04-08-2014 07:55 PM
Super User
Posts: 17,912

Re: Split Dataset by Year from mm/dd/yyyy Format

Why do you want to split up your datasets? This is a commonly asked question, the answer is usually don't do it. 

The manual example, that assumes that your date variable is a SAS numeric variable with a date format, not a character variable:

data Y1998 Y1999;

set have;

if year(date) = 1998 then output Y1998;

else if year(date) = 1999 then output Y1999;

run;

Occasional Contributor
Posts: 14

Re: Split Dataset by Year from mm/dd/yyyy Format

I have millions of records spread over 25 datasets which are all by year.  So I have 25 yearly datasets containing variables A-Y 1999, A-Y 2000, A-Y 2001...And I have one dataset for Variable Z 1999-2001.  I need to split the Variable Z dataset up by year so I can remerge it with Variables A-Y to make A-Z variables for each year in one dataset.

Does that make sense?

The dataset which has one key and 25 years worth of data was uploaded from a text file.  It was a vertical bar delimited file which was just Key (123), Date (01/01/2001), etc. etc...So it didn't specify a format, I think SAS just reads it as a character string, right?

data Y1998 Y1999; --> This creates output datasets?

set have; -->I don't know what a set statement is

if year(date) = 1998 then output Y1998; -->So if the variable name is "datevariable" then it would be "if year(datevariable) = 1998 then output Y1998;"?

else if year(date) = 1999 then output Y1999; -->And I just add as many of these statements as there are years?

run;

Super User
Posts: 17,912

Re: Split Dataset by Year from mm/dd/yyyy Format

Why not merge it directly? 1 million records isn't that big of a dataset.

 

If you're not familiar with SAS at all, you may want to view these videos/tutorials. Or are you using SAS Enterprise Guide?

SAS Starter Kit

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 638 views
  • 0 likes
  • 2 in conversation