## Logical Problem in SAS Code

Occasional Contributor
Posts: 9

# Logical Problem in SAS Code

[ Edited ]

Hi,

i wanted to solve a problem but i am little bit confuse as i am learning SAS.

Here is the algorithm of my code

data operation;
set rule_data;
if satisfy condition then
do;
set data_value ;

if match_condition_of_rule_data_with_data_value then;
peform opeartion on data_value based on rule_data;
end;
run;

when i am trying this,it gives me combine data set and not giving result as i expected as i want value from rule_data_set only and both dataset have different number of observations and variables.

PROC Star
Posts: 396

## Re: Logical Problem in SAS Code

You might want to use a merge instead, it is not possible (at least in an easy fashion) to have multiple datasets open at the samt time in a data step.

data operation;
merge rule_data data_value ;

by common_key;

if match_condition_of_rule_data_with_data_value then;
peform opeartion on data_value based on rule_data;
end;
run;

//Fredrik

Occasional Contributor
Posts: 9

## Re: Logical Problem in SAS Code

[ Edited ]

Hi FredrikE,,

As i mentioned both data set have different number of observations and variables. suppose rule_data has only 50 observations and data_value has 2 millions observation. how is it possible to merge in such condition ? and even if it merged somehow then second challenge i have to apply observation of rule_data on data_value  so i need to iterate 50 times on 2 millions data but if i merge it will iterate 2 millions times it is very confusing for me to understand.

Super User
Posts: 9,818

## Re: Logical Problem in SAS Code

First off, if your learning SAS this is not the the topic to start with, nor is that amount of data.  Both are for more advanced users.  If you make it harder to learn you will more likely fail.

Secondly, with that kind of data, yes, merge probably isn't the way forward.  Working with big data is slightly different, hence why you should learn Base SAS thoroughly before moving onto more advanced topics.

As I posted, you can generate the necessary code:

```data _null_;
set rule_data end=last;  if _n_=1 then call execute('data want; set have;');
call execute(rule);  if last then call execute('run;');
run;```

However it is highly unlikely you will understand what is being done here if you are learning SAS.  What this will do is create a datastep statement after this one has finished, which has the rule statement for each observation from rule table.  That generated code is then run.

Super User
Posts: 9,818

## Re: Logical Problem in SAS Code

There are numerous ways to do this, generate the code from the rule dataset is one that jumps to mind, merging another .  So something like:

```data _null_;
set rule_data;
call execute(...);
run;```

Note I can't provide working code as you have shown no test data in the form of a datastep.

Super User
Posts: 23,980

## Re: Logical Problem in SAS Code

Well, we're making assumptions based on the pseudocode you provided, but it would be better to actually provide sample data and code that we can work off  and see more easily what you want.

My first instinct is to say that what you're doing isn't possible...at least easily...but there are ways. It would be better if you first explained in detail what you want.

Hi,

i wanted to solve a problem but i am little bit confuse as i am learning SAS.

Here is the algorithm of my code

data operation;
set rule_data;
if satisfy condition then
do;
set data_value ;

if match_condition_of_rule_data_with_data_value then;
peform opeartion on data_value based on rule_data;
end;
run;

when i am trying this,it gives me combine data set and not giving result as i expected as i want value from rule_data_set only and both dataset have different number of observations and variables.

Valued Guide
Posts: 596