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

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
decade = floor(year(date)/10) * 10 ;

View solution in original post

7 REPLIES 7
Reeza
Super User

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;

 

 




 

 

Miah
Obsidian | Level 7

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

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

Reeza
Super User
decade = floor(year(date)/10) * 10 ;
Miah
Obsidian | Level 7
This was straight forward what I needed. Thanks!
PGStats
Opal | Level 21

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
novinosrin
Tourmaline | Level 20
data want;
set have;
decade=year(releaseDate)-mod(year(releaseDate),10);
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 7 replies
  • 2845 views
  • 9 likes
  • 5 in conversation