Help using Base SAS procedures

qustion about do loop

Reply
N/A
Posts: 0

qustion about do loop

I need someone to help me to explain the following process:

data sample:

flag num_a num_b
1 0 3
1 0 3
1 0 3
2 4 4

The task is to let num_a = num_b, then set num_b = 0, if flag equals 1.

expected result:

condition num_a num_b
1 3 0
1 3 0
1 3 0
2 4 4

I use a do-end loop to execute it, and coding like this:

if condition = 1 then do;
num_a = num_b;
num_b = 0;
end;

but it doesn't work
The first column is converted correctly, but the rest records are all set to 0. It look like it execue num_b=0 first then execute num_a=num_b;

the result ended up like this:

condition num_a num_b
1 3 0
1 0 0
1 0 0
2 4 4

Even I seperated the do loop steps it gets the same results:

revised code:

if condition = 1 then do;
num_a = num_b;
end;

if condition = 1 then do;
num_b = 0;
end;

Anyone can explain this to me?

Thanks,
Super Contributor
Super Contributor
Posts: 3,174

Re: qustion about do loop

Posted in reply to deleted_user
I suggest you run your code in a SAS DATA step, either using DATALINES; with an INPUT statement, or with a SET (from a previous SAS data member). Then add two statements, as shown below, around your code to generate diagnostics:

DATA _NULL_;
SET ;
PUTLOG ">BEFORE>" / _ALL_;
* your code goes here. ;
PUTLOG ">AFTER>" / _ALL_;
RUN;

With the additional DATA step processing diagnostics, I would expect you can see the processing results and reasoning with your own code. If the problem still exists, paste the entire SASLOG output into your post for the forum subscribers to respond with feedback. It's important that you share your exact DATA step code, using COPY and PASTE for accuracy and purpose.

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: qustion about do loop

Posted in reply to deleted_user
Hi dust,
The below mentioned code will give you the your desired result.
if flag = 1 then do;
num_a = num_b;
num_b = 0;
end;

By running the above code I am getting the below result
flag num_a num_b
1 3 0
1 3 0
1 3 0
2 4 4

not like what you have mentioned.once agin verify the above code,it will give you the your desired result.
N/A
Posts: 0

Re: qustion about do loop

Posted in reply to deleted_user
Thanks all for your kindly response.
Sivas, that's eactly what troubles me. This piece of example is from an old program which had been runing for years. Someone found the problem with the output data recently, and I checked the process and identified the issue. It should work as you pointed out, but it didn't. Could that be possible it is caused by a SAS version problem? I have reasons to believe the code used to worked well. (Of course I can't be 100% sure.) An update SAS version in our unix environment last year may caused the problem. Again, just my guess.

Thanks again for your help
Super Contributor
Super Contributor
Posts: 3,174

Re: qustion about do loop

Posted in reply to deleted_user
As was suggested, share your "executed" SAS code with additional diagnostics in a SASLOG (pasted in your post), otherwise feedback cannot be accurately provided, based on the lack of actual SAS output.

Scott Barry
SBBWorks, Inc.
Ask a Question
Discussion stats
  • 4 replies
  • 127 views
  • 0 likes
  • 2 in conversation