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

I have excel file (attached). It has file name (Test 2020 file) and date variable with actual date such as 1/1/2020 etc.. Some year are incorrect (2045 and 1999), but all should be 2020 not 1999 or 2045. How can I check and fix accordingly? Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
LeonidBatkhan
Lapis Lazuli | Level 10

Hi Emma2021,

 

After you import your Excel file into SAS table, you can clean up your DATE variable. Let's say, you imported your Excel file into SAS dataset HAVE. Then you can do the following manipulation to reconstruct your DATE:

data WANT;
   set HAVE; 

   /* get day and month */
   D = day(DATE);
   M = month(DATE);

/* reconstruct your DATE */ DATE = mdy(M,D,2020); run;

MDY() function calculates SAS date value out of Month, Day and Year.

Hope this helps.

 

View solution in original post

4 REPLIES 4
LeonidBatkhan
Lapis Lazuli | Level 10

Hi Emma2021,

 

After you import your Excel file into SAS table, you can clean up your DATE variable. Let's say, you imported your Excel file into SAS dataset HAVE. Then you can do the following manipulation to reconstruct your DATE:

data WANT;
   set HAVE; 

   /* get day and month */
   D = day(DATE);
   M = month(DATE);

/* reconstruct your DATE */ DATE = mdy(M,D,2020); run;

MDY() function calculates SAS date value out of Month, Day and Year.

Hope this helps.

 

Emma2021
Quartz | Level 8
Instead of 2020 that hard code, there is a way to capture dynamically from the file name? Since there are 100 files--it would be hard to manually write like this.
For example files names are as the following:
Test 2000 Q1 file
Test another 2000 Q2
Test vk 2001 Q1 etc
jimbarbour
Meteorite | Level 14

@Emma2021, how are you importing the files?  Do you have a macro that searches a particular directory and processes all files of a certain type?  

 

At some point in the processing, the file name must be known or you won't be able to process it.  If we can capture the file name, we can extract the year and then add it to the MDY function mentioned in @LeonidBatkhan's reply.

 

Jim

jimbarbour
Meteorite | Level 14

You of course have to first be able to determine the filename inside a Data step, but once you've determined the filename, a parsing routine like the one below can extract the year from the filename.

DATA	WORK.Years_Only;
	DROP	_:;
	LENGTH	_File_Name	$256;

	INFILE	DATALINES	MISSOVER;

	INPUT	_File_Name	$;
	
	_No_More_Characters							=	0;
	_Start_Pos									=	1;

	DO	UNTIL	(_No_More_Characters);
		_Start_Pos								=	ANYDIGIT(_File_Name, _Start_Pos);
		_End_Pos								=	_Start_Pos	+	3;

		IF	_Start_Pos							=	0					OR
			_Start_Pos							>	LENGTH(_File_Name)	OR
			_End_Pos							>	LENGTH(_File_Name)	THEN
			_No_More_Characters					=	1;
		ELSE
			IF	NOTDIGIT(SUBSTR(_File_Name, _Start_Pos, 4))	THEN
				DO;
					_Start_PoS					=	_Start_Pos	+	1;
					IF	_Start_Pos				>	LENGTH(_File_Name)	THEN
						_No_More_Characters		=	1;
				END;
			ELSE
				DO;
					Year						=	SUBSTR(_File_Name, _Start_Pos, 4);
					_No_More_Characters			=	1;
				END;
	END;

DATALINES;
Test_2000_Q1_file
Test_another_2000_Q2
Test_vk_2001_Q1
Test1_2002
Test26_2003_50
Test75_80_2004
No_Year_Here
2005_is_the_year
Howabout2006forayear
;
RUN;

Jim

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 560 views
  • 4 likes
  • 3 in conversation