Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW DO YOU READ A PARTICULAR COLUMN FROM AN EXISTING DATA?

Status
Not open for further replies.

cosmid

Programmer
Feb 14, 2008
73
US
HI...i have posted a message earlier asking how to use the YEAR() funciton. However, when I applied it to the data that I have, it only returns one record, one empty record.

I have an existing data of 600 records with column names VarID, VarName, VarDate, VarInfo, VarGender...etc. The VarDate is in mmddyy10. format.

I want to read the VarDate column and save this as a new data. I also want to apply the YEAR() to the VarDate column. But I only get 1 empty rec.

Here is the code that I wrote:

libname mydata 'C:\work\analysis';
libname myfmts 'C:\work\analysis';

data mydata.newFiles;
infile 'C:\work\analysis\rdata.dat';
input DateEvent mmddyy10.;
y=year(DateEvent);
run;

proc print data=mydata.newFiles;
options fmtsearch=(myfmts work);
var y;
run;

What did i do wrong? I just want to read that date column and extract the year from the value so I can get a count of the recs for each year.

Thanks a lot for the help!
 
Have you tried to open/view the dataset mydata.newFiles after you import it? (Just a thought to make sure the import step worked properly.)

I took the code you provided and made my own "rdata" file.

Code:
data rdata; 
	input DateEvent mmddyy10.; 
	cards; 
07-01-2008 
07-02-2008 
07-12-2008 
07-13-2008 
07-14-2008 
07-15-2008 
; 

data newFiles;
set rdata;
y=year(DateEvent);
run;

proc print data=newFiles;
var y;
run;

This worked fine for me.
The results from the proc print:
Code:
Obs      y
1     2008
2     2008
3     2008
4     2008
5     2008
6     2008
 
What does your log look like?
I imagine the problem is not the year function usage, but a problem reading the file.
Comment out the "y=year(DateEvent);" line (insert an * at the start of the line) then re-run.
Usually I write these input steps like this:-
Code:
data mydata.newFiles;
    infile 'C:\work\analysis\rdata.dat'
           recfm=v lrecl=256 missover;
    informat dateevent mmddyy10.;
    input DateEvent;
    * y=year(DateEvent);
run;
Get the input working first, then work on the year part.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
dblan (MIS):

Thank you for trying out the code with new data creation. However, your data has only 1 column of raw data. I have made similar tests earlier and they all works fine. But when it comes to read an existing data with multiple columns, it just doesn't work. I need to know how to create a new dataset with 1 variable modified from the existing data set. I need to get just the year from the variable VarDate so I can count how many recs exists for each year. I made this work by using proc print and copy-pasted the output from the output. Then I used like what you did, using an input statement, and it works fine. But I wanted to know how to do this when reading from a file. I will do a lot of these simple analysis in the future, I do not want to copy and paste all the data everytime. But thanks again for your time and help! I appreciate it!
 
ChrisW75 (Programmer):

Thanks a lot for the sample code. You are right. The problem is reading the file. I can not read in the date file from the data set. Here is the code:

libname mydata 'C:\work\analysis';
libname myfmts 'C:\work\analysis';

data mydata.newFiles;
infile 'C:\work\analysis\taxidata.dat'
recfm=v lrecl=256 missover;
informat dateevent mmddyy10.;
input DateEvent;
*y=year(DateEvent);
run;

proc print data=mydata.newFiles;
options fmtsearch=(myfmts work);
run;

------------------------------------------------------------
Sorry, I havn't figured out how to post code here in a code window like yours.

Here is the log:


1504 libname mydata 'C:\work\analysis';
NOTE: Libname MYDATA refers to the same physical library as MYFMTS.
NOTE: Libref MYDATA was successfully assigned as follows:
Engine: V9
Physical Name: C:\work\analysis
1505 libname myfmts 'C:\work\analysis';
NOTE: Libname MYFMTS refers to the same physical library as MYDATA.
NOTE: Libref MYFMTS was successfully assigned as follows:
Engine: V9
Physical Name: C:\work\analysis
1506 filename myfile 'C:\work\analysis';
1507
1508 data mydata.newFiles;
1509 infile 'C:\work\analysis\rdata.dat' recfm=v lrecl=256 missover;
1510 informat dateevent mmddyy10.;
1511 input DateEvent;
1512 *y=year(DateEvent);
1513 run;

NOTE: The infile 'C:\work\analysis\rdata.dat' is:
File Name=C:\work\analysis\rdata.dat,
RECFM=V,LRECL=256

NOTE: Invalid data for dateevent in line 1 1-87.
RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0

1 CHAR ............Âê.`³..Ͻ’...Ç1Œ...."".2"..2................"".2"..2.2."".>.............SAS FILETAXIDATA
ZONE 000000000000CE86B11CB9000C38111122032003000000000000111122032003030220300100000000005452444454544454
NUMR 0000000000002A10341FD280971C8F01220221224000000000318F0122022122421220E0003100000000313069C541894141

101 DATA ..&¿æÎÖA..&¿æÎÖA..... ÌÀ..... ÌÀ....
ZONE 2222222222222222222222222222222222222222222222222222222244542222102BECD4102BECD4000002CC000002CC0000
NUMR 0000000000000000000000000000000000000000000000000000000041410000946F6E61946F6E61000000C0000000C00400

201 .@..)...........9.0101M3XP_PRO..........................
ZONE 04002000000000003233334355555400000000000000000000000000
NUMR 00009000000000009E0101D380F02F00000000000000000000000000
dateevent=. _ERROR_=1 _N_=1
NOTE: 1 record was read from the infile 'C:\work\analysis\rdata.dat'.
The minimum record length was 256.
The maximum record length was 256.
One or more lines were truncated.
NOTE: The data set MYDATA.NEWFILES has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.00 seconds

1526
1527 proc print data=mydata.newFiles;
1528 options fmtsearch=(myfmts work);
1529 run;

NOTE: There were 1 observations read from the data set MYDATA.NEWFILES.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

------------------------------------------------------------

The output only got 1 observation of a missing data for dateevent. Like you said, the problem is reading in the file. I am not sure where I did wrong. The format of the raw data is: VarID $ 5 VarName $ 50 VarPage $ 15 Dateevent mmddyy10.

And there are more data after dateevent. Varpage is there, but the entire column is empty. Do I need to use a @pointer to read in the dateevent column? I tried that and it did not work. I tried like 20 different positions with the @.

Thanks a lot for your time and help! Really appreciate this!

 
It looks like it's read in a line of garbage data, possibly a header.
Either that or the data file is not in Ascii format.
Can you copy and paste say the first 10 lines of the data file here, open it in Notepad or something similar to view it.


Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
I can open the data using SAS. But when I try to open the data with notepad, it comes out to be pretty messed up:

Âê?`³Ͻ’ Ç1Œ"" 2"2 "" 2"22"" >  SAS FILEPRACTICEDATA DATA þÔh‰UÜÖAþÔh‰UÜÖA ÌÀ ÌÀ  @ ) 9.0101M3XP_PRO þÔh‰²š첚첚ì ?¾Ø þÔh‰UÜÖA œ¾Ø ß j d > à > ä< 0 È7   Ô4 ô  p0 d  L/ $  / 4 ä. 4 °. 4 |. 4 H. 4 . 4 à- 4 ¬- 4 x- 4 D- 4 - 4 Ü, 4 ¨, 4 t, 4 @, 4 , 4 Ø+ 4 ¤+ 4 p+ 4 <+ 4 + 4 Ô* 4  * 4 l*
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top