turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Data step calculations

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-30-2008 02:26 PM

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,

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,

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to deleted_user

09-30-2008 07:18 PM

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to deleted_user

10-01-2008 09:13 AM

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Patrick

10-01-2008 02:00 PM

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.