View previous topic :: View next topic
|
Author |
Message |
santosh_g
New User
Joined: 14 Aug 2011 Posts: 24 Location: india
|
|
|
|
Hello
Can anyone please help me understand why the output is like below:
The second record of Jone is also getting deleted from SAS dataset.
My understanding is that SAS is reading two records as one because of NUM2 being ".". How can I handle this (to remove Edgar and Include all Jone).
Code: |
//SYSIN DD *
DATA TEMP ;
INPUT @001 EMPID 6.
@007 EMPNAME $10.
@017 EMPAGE 2.
@019 NUM 2. ;
IF EMPNAME = 'EDGAR '
THEN DO ;
INPUT @019 NUM2 2. ;
DELETE ;
END ;
OUTPUT RI863B ;
DATALINES;
043006JONE 3111
043006EDGAR 3211
043006EDGAR 3311
043006EDGAR 3411
043006EDGAR 3511
043006EDGAR 3611
043006JONE 3711
043006EDGAR 3811
043006EDGAR 3911
043006EDGAR 3011
043006EDGAR 4111
043006EDGAR 4211
043006EDGAR 4311
043006EDGAR 4411
;
RUN ;
PROC PRINT DATA=TEMP ;
RUN ;
|
Output:
Code: |
The SAS System
Obs EMPID EMPNAME EMPAGE NUM NUM2
1 43006 JONE 31 11 .
|
|
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
Quote: |
My understanding is that SAS is reading two records as one because of NUM2 being ".". |
Your understanding is very wrong. You are telling SAS to read from a separate line (record) every time it finds EMPNAME = 'EDGAR '. Hence it reads the first line and keeps it. The second line is read, then the third line is read inside the IF statement and all that data is deleted. The fourth line is read, then the fifth line is read inside the IF statement and again the data is deleted .... etc. SAS allows you to re-read a data line but you must use a trailing @ sign in your INPUT statement.
Something like this is what you want:
Code: |
DATA TEMP ;
INPUT @001 EMPID 6.
@007 EMPNAME $10.
@017 EMPAGE 2.
@019 NUM 2. @ ;
IF EMPNAME = 'EDGAR '
THEN DELETE ;
OUTPUT RI863B ; |
Why do you want to read columns 19-20 as 2 separate variables? NUM and NUM2 will have the same values every time.
Why do you read NUM2 and then immediately delete the observation?
Why do your DATA step and your OUTPUT have different names?
I would usually code this up as
Code: |
INPUT @007 EMPNAME $10. @ ;
IF EMPNAME =: 'EDGAR' THEN DELETE;
INPUT @001 EMPID 6.
@017 EMPAGE 2.
@019 NUM 2. ; |
With SAS you don't have to read data from column 1 on - you can pick and choose. And I'll let you figure out the =: construct yourself (hint: it's in the SAS manual). |
|
Back to top |
|
|
santosh_g
New User
Joined: 14 Aug 2011 Posts: 24 Location: india
|
|
|
|
Thank you Robert.
Quote: |
Your understanding is very wrong. |
Yes. I am currently learning SAS and was testing few code. I am aware of reading data from any location. But I was not sure about @.
Thank you for your help & info.
Best Regards, |
|
Back to top |
|
|
vasanthz
Global Moderator
Joined: 28 Aug 2007 Posts: 1742 Location: Tirupur, India
|
|
|
|
Santhosh,
Robert already gave you the right info about using trailing @ to read a records more than once.
You showed how your input file looks like. If you could show us what exactly is the output for the above mentioned input datalines, then we could help you with the logic of SAS.
Regards, |
|
Back to top |
|
|
|