Not applicable
Posts: 0

# Data step calculations

Hi,

I've two data sets file1 and file2

file1 contains variables X and Z
file2 contains variables X and Y

I've the values of X in both the datasets and I also have the values of Y in file2.

Now i've to write a code where the condition is:

where file1:X=file2:X then Z=Y*1000

Can you please tell me how I should be coding this.

Thanks,
Not applicable
Posts: 0

## Re: Data step calculations

It sounds like from your post that file1:X is not always equal to file2:X, but sometimes it is...and if it is are equal you want Z to be overwritten with Y*1000. If that is the case

merge the datasets by X that will take care of the "where file1:X = file2:X" part.

then knowing that the merge will fill any non-equivalent data with missing data
you can use: if Y ^= . and Z ^= . then Z = Y * 1000;

data file3;
merge file1 file2;
by X;
if Y ^= . and Z ^= . then Z = Y * 1000;
run;

caution you still may have missing data both Y and Z.

johnny
Posts: 4,736

## Re: Data step calculations

As I understand it the calculation should only happen if the observations match. To control that "in" can be used. Based on the already given example:

data file3;
merge file1 (in=MyDS1) file2 (in=MyDS2);
by X;

/* do the following only if there is a matching record */
if MyDS1 and MyDS2 then Z = Y * 1000;
run;

In case that Y is missing, Z will be missing as well. If you don't want that then just add a bit more logic (have a look at "ifn" in the SAS OnlineDoc).

HTH
Patrick
Not applicable
Posts: 0

## Re: Data step calculations

I used the "not equal" for both cases...meaning if there is a value for both Y and Z (implies observation of X matches) then overwrite Z. Getting around the creation of new variables. Pretty sure both will work similarly, I've never used the "in" in that way though.
Discussion stats
• 3 replies
• 147 views
• 0 likes
• 2 in conversation