Help using Base SAS procedures

Update/fix the date

Reply
N/A
Posts: 0

Update/fix the date

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;
Valued Guide
Posts: 2,177

Re: Update/fix the date

Posted in reply to deleted_user
Assuming FilmDate is a date-time value, what month year and time should default for those cases you want to replace with year=1977?
N/A
Posts: 0

Re: Update/fix the date

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.
N/A
Posts: 0

Re: Update/fix the date

Posted in reply to deleted_user
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;
Valued Guide
Posts: 2,177

Re: Update/fix the date

Posted in reply to deleted_user
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;
N/A
Posts: 0

Re: Update/fix the date

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?
N/A
Posts: 0

Re: Update/fix the date

Posted in reply to deleted_user
I mean DENTAL2_2.
Valued Guide
Posts: 2,177

Re: Update/fix the date

Posted in reply to deleted_user
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
N/A
Posts: 0

Re: Update/fix the date

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?
Valued Guide
Posts: 2,177

Re: Update/fix the date

Posted in reply to deleted_user
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
Super Contributor
Super Contributor
Posts: 3,174

Re: Update/fix the date

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.
N/A
Posts: 0

Re: Update/fix the date

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....
Super Contributor
Super Contributor
Posts: 3,174

Re: Update/fix the date

Posted in reply to deleted_user
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
N/A
Posts: 0

Re: Update/fix the date

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.?
Super Contributor
Super Contributor
Posts: 3,174

Re: Update/fix the date

Posted in reply to deleted_user
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.
Ask a Question
Discussion stats
  • 15 replies
  • 218 views
  • 0 likes
  • 4 in conversation