BookmarkSubscribeRSS Feed
mnew
Calcite | Level 5
Greetings:
Trying to figure out an efficient way to read in a very simple CSV file by using Data step (part of my practice for input, infile, informat etc.)... I know how to do this using the import wizard but I simply could not find the way to use a data step for the date field.

CSV data: (comma delimited)
Abby 1/1/2000
Andy 2/1/2001

Here is the code I tried first, which did not give an error message but date values were showing up as missing.

Filename csvTest 'C:\Documents and Settings\Desktop\SAS_Learning\review\test.csv';

data work.testwithoutyear;
infile csvtest dsd missover;
input first_name $ order_date mmddyy10.;
run;

The second puzzle: I added a year value as the third variable. (e.g. Abby, 1/1/2000, 2000).
Then I ran the code with a slightly modified input statement (input first_name $ order_date mmddyy10. order_year 😉 . This time the SAS dataset has the right order_date but the year showed up as 0 for the example.

Any advice? Do you think I should only use Proc Import to read in CSV and give up the Data step?

Thank you!
4 REPLIES 4
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello Mnew,

I made some experimenting and found that this modification of your code and csv-file works.

a) code (delimiter and format )

data work.testwithoutyear;
infile csvtest delimiter=',' missover;
input first_name $ order_date mmddyy10. year;
format order_date mmddyy10.;
run;

Data: (2 blanks before dates because format requires 10 symbols)

Abby, 1/1/2000,2000
Andy, 2/1/2001,2001

Sincerely,
SPR
mnew
Calcite | Level 5
Thank you. Tested your solution. It worked fine. Agree with you that it's the informat!
Cynthia_sas
SAS Super FREQ
Hi:
If you use the colon modifier for the INPUT statement, you will be instructing SAS to read the data using a specific INFORMAT (in this case MMDDYY10.). For more about the colon modifier for INPUT, read the documentation topic entitled: "Modified List Input" which shows examples of the colon format modifier:
http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a003209907.htm

cynthia
[pre]
data work.testdata;
infile datalines delimiter=',' ;
input first_name $ order_date : mmddyy10. year;
format order_date mmddyy10.;
return;
datalines;
Abby,1/1/2000,2000
Andy,2/1/2001,2001
;
run;

ods listing;
proc print data=testdata;
run;
[/pre]
mnew
Calcite | Level 5
Thank you. I added : to my codes to read from the two CSV files. They worked!

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!

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
  • 10716 views
  • 2 likes
  • 3 in conversation