DATA Step, Macro, Functions and more

Indicate decades from dates

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

Indicate decades from dates

[ Edited ]

I am working with the data set TopGrossingAlbumsR.txt, triying to create a new character variable that indicates the decade when the album was released. 

 

I am not sure about the right way to approach this question. How should I initialize the char variable? how could I specify the date format in the IF statement and finally, how could I assign the value of decade to my new variable?

 

The values of 'ReleaseDate' are written in the format: 30NOV1982 

I want my new char variable 'Decade' to indicate 1980 for this example.

 

Here it is my code and I have attached the data set below:

 

DATA music;
INFILE '/folders/myfolders/TopGrossingAlbumsR.txt' dsd firstobs=2;
LENGTH Album $80. Artist $50. Genre $20.;
INFORMAT ReleaseDate ANYDTDTE.;
FORMAT ReleaseDate DATE9.;

INPUT Album Artist ReleaseDate TotalCertifiedCopies ClaimedSales Genre;
RUN;

PROC PRINT DATA = music;
FORMAT ReleaseDate DATE. ;
RUN;

DATA music;
SET music;
Decade = ' ';

IF ReleaseDate = ' ' THEN Decade = YYYY;

IF Genre = 'Metal' THEN Genre = 'Rock';
IF Genre = 'Grunge' THEN Genre = 'Rock';
IF Genre = 'Soundtrack' THEN Genre = 'Other';
IF Genre = 'Country' THEN Genre = 'Other';
RUN;

PROC PRINT DATA = music;
RUN;

 

 


Accepted Solutions
Solution
‎03-04-2018 11:46 PM
Super User
Posts: 22,844

Re: Indicate decades from dates

decade = floor(year(date)/10) * 10 ;

View solution in original post


All Replies
Super User
Posts: 22,844

Re: Indicate decades from dates

First explain in words what you want. 

Then show the data, then the code. 

 

So, if you have 1990, what do you want, 90? 1990?

If 2000, do you want 00? or 2000?

How will you tell 2010 from 1910?

 


Miah wrote:

I am working with the data set TopGrossingAlbumsR.txt, triying to create a new character variable that indicates the decade when the album was released. 

 

I am not sure about the right way to approach this question. How should I initialize the char variable? how could I specify the date format in the IF statement and finally, how could I assign the value of decade to my new variable?

 

Here it is my code and I have attached the data set below:

 

DATA music;
INFILE '/folders/myfolders/TopGrossingAlbumsR.txt' dsd firstobs=2;
LENGTH Album $80. Artist $50. Genre $20.;
INFORMAT ReleaseDate ANYDTDTE.;
FORMAT ReleaseDate DATE9.;

INPUT Album Artist ReleaseDate TotalCertifiedCopies ClaimedSales Genre;
RUN;

PROC PRINT DATA = music;
FORMAT ReleaseDate DATE. ;
RUN;

DATA music;
SET music;
Decade = ' ';

IF ReleaseDate = ' ' THEN Decade = YYYY;

IF Genre = 'Metal' THEN Genre = 'Rock';
IF Genre = 'Grunge' THEN Genre = 'Rock';
IF Genre = 'Soundtrack' THEN Genre = 'Other';
IF Genre = 'Country' THEN Genre = 'Other';
RUN;

PROC PRINT DATA = music;
RUN;

 

 




 

 

Contributor
Posts: 22

Re: Indicate decades from dates

The values of 'ReleaseDate' are written in the format: 30NOV1982 

I want my new char variable 'Decade' to indicate 1980 for this example.

Solution
‎03-04-2018 11:46 PM
Super User
Posts: 22,844

Re: Indicate decades from dates

decade = floor(year(date)/10) * 10 ;
Contributor
Posts: 22

Re: Indicate decades from dates

This was straight forward what I needed. Thanks!
Super User
Posts: 9,563

Re: Indicate decades from dates


Reeza wrote:
decade = floor(year(date)/10) * 10 ;

Perfect application of the KISS principle.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Esteemed Advisor
Posts: 5,398

Re: Indicate decades from dates

Do this the cool way with intnx("year10", ...)

 


DATA music;
   SET music;
   Decade = intnx("year10", releaseDate, 0);
   length decadeTxt $4;
   decadeTxt = cats("'", put(decade, year2.), "s");
   IF Genre = 'Metal' THEN Genre = 'Rock';
   IF Genre = 'Grunge' THEN Genre = 'Rock';
   IF Genre = 'Soundtrack' THEN Genre = 'Other';
   IF Genre = 'Country' THEN Genre = 'Other';
   format decade year2.;
RUN; 
PG
PROC Star
Posts: 1,309

Re: Indicate decades from dates

data want;
set have;
decade=year(releaseDate)-mod(year(releaseDate),10);
run;
☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 397 views
  • 9 likes
  • 5 in conversation