BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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,
3 REPLIES 3
deleted_user
Not applicable
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;

your end solution should be:

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
Patrick
Opal | Level 21
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
deleted_user
Not applicable
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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 990 views
  • 0 likes
  • 2 in conversation