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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 767 views
  • 0 likes
  • 2 in conversation