View previous topic :: View next topic
|
Author |
Message |
asimkp
New User
Joined: 24 Aug 2008 Posts: 4 Location: Bangalore,India
|
|
|
|
Hello,
I need some help for updating a record in SAS dataset.
Below are the details -
1. Copy the SAS Dataset
2. Update the record in one of the member of the SAS Dataset.
The structure of the member is pretty simple. It has only two columns(Variables) and 37 observation. I want to update the value for last record.
Currently, say the value for this record is
I want to update the value in col2 from 123 to 345.
I tried the following code, but in vain -
Code: |
PROC COPY IN=INPUT1 OUT=OUTPUT1 MEMTYPE=ALL;
RUN;
DATA OUTPUT1.BAN;
SET INPUT1.BAN;
IF COL1 EQ 'ABC';
COL2 = '345';
RUN; |
With this code, instead of copying all the 37 records from BAN, only the record 'ABC' is being copied with a value of 345.
Request you to please help me with this. |
|
Back to top |
|
|
Phrzby Phil
Senior Member
Joined: 31 Oct 2006 Posts: 1042 Location: Richmond, Virginia
|
|
|
|
Your line
is called a subsetting IF. Only obs for which it is true continue with the next line; others resume at the top with the next obs.
You want
Code: |
IF COL1 EQ 'ABC'
THEN COL2 = '345';
|
This line runs for all obs, and only the ones passing the test have COL2 changed. Alll then continue to be written to the output.
This is a nice feature of SAS, but also an excellent example of the dangers lurking in SAS. What would be a syntax error in other languages (the ";" after the condition) is good SAS syntax, with the meaning I described. |
|
Back to top |
|
|
expat
Global Moderator
Joined: 14 Mar 2007 Posts: 8797 Location: Welsh Wales
|
|
|
|
Well, first of all you did not state categorically that the value for COL1 would always be 'ABC' for the last record, nor did you state that 'ABC' could not be repeated within the sas dataset.
Of course you could have read the manual for data step options and found out all about the NOBS option.
As I'm in a really good mood today
Code: |
data whatever;
set whatever nobs=eof1;
if _N_ = eof1 then col2 = 'xxx';
output;
run; |
|
|
Back to top |
|
|
asimkp
New User
Joined: 24 Aug 2008 Posts: 4 Location: Bangalore,India
|
|
|
|
Hello Phil & Expat,
Thanks a lot for all your help...
Removing the ';' has helped resolve the problem.
We can close this thread now. |
|
Back to top |
|
|
|