BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I am running the data set. It has dates where the year is not within the specified range so for those dates, I want to replace the incorrect year with the value 1977 (which is the mid year for cycle=3). So basically I want to update FILMDATE with 1977 as the year. How do I do that?

DATA INCORRECT1_3;
SET DENTAL1_2;
DATE=DATEPART(FILMDATE);
YEAR=YEAR(DATE);
WHERE CYCLE=3;
IF (YEAR LT 1975) OR (YEAR GT 1980) THEN *UPDATE FILMDATE WITH YEAR 1977;;
RUN;

PROC PRINT DATA=INCORRECT1_3;
RUN;
15 REPLIES 15
Peter_C
Rhodochrosite | Level 12
Assuming FilmDate is a date-time value, what month year and time should default for those cases you want to replace with year=1977?
deleted_user
Not applicable
Assuming FilmDate is a date-time value, what month year and time should default for those cases you want to replace with year=1977?
Yes, FILMDATE is date-time value. I want the new FILMDATE variable to have only the date (no time component) with the year 1977.
deleted_user
Not applicable
Forget the earlier posts. The YEAR column shows 1980, which is what I want. Now I want DATE to show 1980 as well. How do I do that?

DATA INCORRECT2;
LENGTH TMISSING $ 8;
SET DENTAL2_2;
DATE=DATEPART(FILMDATE);
YEAR=YEAR(DATE);
FORMAT DATE DATE9.;
IF (YEAR LT 1978) OR (YEAR GT 1982);
IF (YEAR LT 1978) THEN YEAR=1980;
IF (YEAR GT 1982) THEN YEAR=1980;
RUN;
PROC PRINT DATA=INCORRECT2;
RUN;
Peter_C
Rhodochrosite | Level 12
look at the on-line doc for the put function, and format DTYEAR.

From a datetime value it will return the "year".

If you want to extend your learning, look at PROC FORMAT where you can define you own format which would use the DTyear format for time-stamps in your "valid" range and '1980' for all other time-stamp values.

With that user format created as VldYr. your code would reduce to

DATA film_years ;
SET DENTAL2_2 ;
year = put( FILMDATE, vldYr. ) ;
RUN;

PROC PRINT DATA= film_years ;
where year = '1980' ;
RUN;

Alternatively, eliminate the data step with enhanced where statement like:
PROC PRINT DATA= film_years ;
where put( FILMDATE, vldYr. ) = '1980' ;
RUN;
deleted_user
Not applicable
Alternatively, eliminate the data step with enhanced where statement like:
PROC PRINT DATA= film_years ;
where put( FILMDATE, vldYr. ) = '1980' ;
RUN;

I can try that. One question though, how will this PROC PRINT step know to take from data set DENTAL1_2?
deleted_user
Not applicable
I mean DENTAL2_2.
Peter_C
Rhodochrosite | Level 12
where the data comes from is defined by the DATA= option of the PROC PRINT statement, So you identified my error. (It was not intentional, just wrong).
The print step might work better, defined like[pre] PROC PRINT DATA= DENTAL2_2 ;
where put( FILMDATE, vldYr. ) = '1980' ;
RUN; [/pre]but you still have to create the format VldYr with PROC FORMAT. The on-line doc for PROC FORMAT has relevant "Example 3: Writing a Format for Dates Using a Standard SAS Format" at http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/a002473487.htm (of course, your inputs are date-time values rather than date values, so you will be using date-time constants in the VALUE statement ranges == more good SAS learning

PeterC
deleted_user
Not applicable
Okay, I get that but for PROC FORMAT, what do I put for VALUE statement? I want the year of DATE variable to show as 1980 so how do I write that in VALUE statement?
Peter_C
Rhodochrosite | Level 12
the purpose of the user-format I propose is to convert a timestamp to a year - either the year of the timestamp or 1980.
78 - 82 are valid years = use DYyear. format
other should be set to constant 1980
Because your "filmDate" variable is a timestamp, you need to use a range of timestamp constants for the valid years.
It is worth looking up the doc for "dt" constants.
Here is how it is used in a VALUE statement
VALUE VldYr (round)
"1jan1978:0:0"dt - '31Dec1982:23:59:59.999'dt = [dtyear4.]
other = '1980'
;
you can do the rest from the manusl
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
To update a SAS DATE variable (change the year-portion), you can look at using either the MDY function (after extracting the MONTH and DAY using these same-named functions in a DATA step. Also, the INTNX function may be suitable if you are looking to increment/decrement the year portion -- using the "SAMEDAY" argument with INTNX.

Check the SAS DOC (online and at the SAS support website) for these functions.

Scott Barry
SBBWorks, Inc.
deleted_user
Not applicable
I'm new to SAS so what you suggested seem complicated for me. Is there a simpler way to do it? I only want to replace 1980 for all years in DATE variable....
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Have you looked at using the SAS DATA step and IF/THEN ; statement processing to perform data manipulation with SAS? It's not so complicated.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search arguments, this topic/post:

introduction data step programming site:sas.com

mdy function data variables site:sas.com
deleted_user
Not applicable
I got the YEAR to show me 1980, I want all DATE entries to update its year to 1980 so... what if I do DDMM1980.?
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
You need to take a serious look at the SAS Language Elements DOC, specifically the functions YEAR, MONTH, DAY (extract the date components as assigned variables), and also MDY (used to create a SAS variable - here is where you can supply a year-value as the third argument).

Suggest you work up a SAS program, test it, and then come back to the forum for feedback, if you have challenges.

Tip: use the PUTLOG _ALL_; command in your DATA step to display all variable values in the SAS log.


Scott Barry
SBBWorks, Inc.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 15 replies
  • 2072 views
  • 0 likes
  • 4 in conversation