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;
decade = floor(year(date)/10) * 10 ;
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;
The values of 'ReleaseDate' are written in the format: 30NOV1982
I want my new char variable 'Decade' to indicate 1980 for this example.
decade = floor(year(date)/10) * 10 ;
@Reeza wrote:
decade = floor(year(date)/10) * 10 ;
Perfect application of the KISS principle.
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;
data want;
set have;
decade=year(releaseDate)-mod(year(releaseDate),10);
run;
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.
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.