BookmarkSubscribeRSS Feed
sravanpathu
Calcite | Level 5

Hi SAS Wrold,

I had Below dataset called Test.

A     B     C            ------------------ Variables

a     1     2

b     2     4

c     3     6

d     4     0

I have written program

Data Test1;

     Set Test;

     If A='d' Then Do;

          If B <=0 Then Do;

          Drop B;

          end;

          If C <=0 Then Do;

          Drop C;

          end;

     end;

run;

When am executing above program in output dataset Test1 both Variable  B and C got droped. Can anybody help me how it is working.


Regards,

SAS_Sravan

4 REPLIES 4
ballardw
Super User

Drop statements are not conditional, if in the code they will drop the variable. Since the purpose of Drop is to remove the Variable from the dataset that makes sense. You may actually want to set B and C to missing values.

Maybe you should provide an example of your input data and the desired outcome and possibly describe what you are going to do with the resulting data.

esjackso
Quartz | Level 8

Do you want to remove the row instead? you can conditionally delete observations using if b<=0 then delete.

Also you dont need do loop for one action.

EJ

mike629
Calcite | Level 5

IF won't affect the execution of DROP here.

Patrick
Opal | Level 21

During compilation phase of a data step SAS scans the code to determine and create all variables - also for the output data set(s). Think of it as the "header" of the data set (descriptor information).

SAS(R) 9.2 Language Reference: Concepts, Second Edition

SAS data sets are orthogonal so you can't have a variable in one row and then not in another row. That's why it makes sense that DROP can't be used conditionally during the execution phase of the data step but is used during compilation phase.

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
  • 1146 views
  • 0 likes
  • 5 in conversation