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

 

On my txt file the data in position 1 is formatted as 20020408.(yyyymmdd)

 

I want to import the academic_date in the same format (yyyymmdd)  but as a date value in sas. How can I do this?

 

DATA import_file;

 

INFILE "K:\DATA\academic.file18.txt"

 

INPUT

@1 ACADEMIC_DATE $8.;

RUN;

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Use 

yymmddn8.;

 format

 

 

DATA import_file;

 

INFILE "K:\DATA\academic.file18.txt"

 

INPUT

@1 ACADEMIC_DATE yymmdd8.;

format ACADEMIC_DATE  yymmddn8.;

RUN;

View solution in original post

19 REPLIES 19
novinosrin
Tourmaline | Level 20

why aren't you importing aka reading the date with an informat  yymmdd10.?

 

DATA import_file;

 

INFILE "K:\DATA\academic.file18.txt"

 

INPUT

@1 ACADEMIC_DATE yymmdd10..;

format ACADEMIC_DATE  yymmdd10.;

RUN;

 

AP101
Obsidian | Level 7

The data is 8 characters in length. When I use the yymmdd10. I get an error message "invalid data for academic_date in line 1-9."

novinosrin
Tourmaline | Level 20

Post samples of your data so that we can test from our end. Also post the required output  you want

AP101
Obsidian | Level 7

As the length is 8 characters I used yymmdd8 and the result is 02-04-08. How can I remove get it to read as 20020408? no dashes

novinosrin
Tourmaline | Level 20

Use 

yymmddn8.;

 format

 

 

DATA import_file;

 

INFILE "K:\DATA\academic.file18.txt"

 

INPUT

@1 ACADEMIC_DATE yymmdd8.;

format ACADEMIC_DATE  yymmddn8.;

RUN;

AP101
Obsidian | Level 7

That did it. Thank you.

AP101
Obsidian | Level 7

@novinosrin How can I read in 2018 as a numeric date.

 

I've tried:

 

DATA import_file;

 

INFILE "K:\DATA\academic.file18.txt"

 

INPUT

 

@ 10    year    year4.;

 

novinosrin
Tourmaline | Level 20

if the value is a number, it is considered standard data and so you do need a special instruction aka informat to read. 

So in your example 2018 can be directly read as year value

 

data want;

input year;

cards;

2018

;

 

No need to supply any informat 

 

AP101
Obsidian | Level 7
i'm reading the year 2018 into sas from a text file. I want to format it as a date.
novinosrin
Tourmaline | Level 20

Sure, that doesn't make a difference whether you read from txt or csv or whateever. If your value is 2018 which is a year value and if you have logic date and month values in the same txt file, you could use mdy function to compute a sas date and format it the way you want. 

 

What date do you think you would want to assign to just 2018?

AP101
Obsidian | Level 7
I only want to read in 2018 from the txt file as a numeric value.

it's in position @10
novinosrin
Tourmaline | Level 20

did you try 

input @10 year 

 

AP101
Obsidian | Level 7
Tried:
input

@ 10 year 4.;

that worked. Thanks again.
Tom
Super User Tom
Super User
I don't understand the question. If you read it in using an INFORMAT it does not care what delimiter is used in the source text, the result is still a number (number of days since 1960). Then you can attach any date format you want to your variable once you have it as a date instead of as text. Personally I wouldn't use one that had month or day first as whichever order you pick will confuse half of your audience. Do use a format of DATE9. or YYMMDD10. and the values will be clearer.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 19 replies
  • 3335 views
  • 0 likes
  • 4 in conversation