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
- /
- Re: why I am getting this answer?

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
- Permalink
- Email to a Friend
- Report Inappropriate Content

2 weeks ago

data test;

input x;

datalines;

1

2

3

4

5

;

run;

proc print data=test;

run;

data test1;

set test;

if x=1 then y=10;

if x=2 then y=20;

if x=3 then y=30;

if x=4 then y=40;

else y=5000;

run;

Answer which i am getting is:

X Y

1 | 1 | 5000 |

2 | 2 | 5000 |

3 | 3 | 5000 |

4 | 4 | 40 |

5 | 5 | 5000 |

Can someone please explain this to me? I am able to solve "else if" but facing problem in this type.

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

Posted in reply to mohitraj4u

2 weeks ago - last edited 2 weeks ago

Each IF statement is evaluated independently, but along with each ELSE.

Only you’re last IF is connected to the ELSE condition, The ELSE in this case resets all the previous results because they’re not 4.

Change all of your IF to ELSE IF, except the first one, or use a SELECT statement instead.

mohitraj4u wrote:

data test;

input x;

datalines;

1

2

3

4

5

;

run;

proc print data=test;

run;

data test1;

set test;

if x=1 then y=10;

if x=2 then y=20;

if x=3 then y=30;

if x=4 then y=40;

else y=5000;

run;

Answer which i am getting is:

X Y

1 1 5000

2 2 5000

3 3 5000

4 4 40

5 5 5000

Can someone please explain this to me? I am able to solve "else if" but facing problem in this type.

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

Posted in reply to Reeza

2 weeks ago

And for a different way to evaluate multiple values of a variable:

data test1; set test; select (x); when (1) y=10; when (2) y=20; when (3) y=30; when (4) y=40; otherwise y=5000; end; run;

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

Posted in reply to mohitraj4u

2 weeks ago

PDV understanding is very important and handy: try using put like the following to gauge what's happening

data test;

input x;

datalines;

1

2

3

4

5

;

run;

data test1;

set test;**if x=1 then y=10;****put x= y=;****if x=2 then y=20;****put x= y=;****if x=3 then y=30;****put x= y=;****if x=4 then y=40;****else y=5000;****put x= y=;**

run;

**x=1 y=10****x=1 y=10****x=1 y=10****x=1 y=5000****x=2 y=.****x=2 y=20****x=2 y=20****x=2 y=5000****x=3 y=.****x=3 y=.****x=3 y=30****x=3 y=5000****x=4 y=.****x=4 y=.****x=4 y=.****x=4 y=40****x=5 y=.****x=5 y=.****x=5 y=.****x=5 y=5000**

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

Posted in reply to novinosrin

2 weeks ago

@novinosrin I would suggest adding some text to your PUT statement, such as CHECK1, CHECK2. It makes it easier to trace. Your solution is a great suggestion on how to view what’s happening.

novinosrin wrote:

data test1;

set test;if x=1 then y=10;put 'CHECK1' x= y=;if x=2 then y=20;put 'CHECK2' x= y=;if x=3 then y=30;put x= y=;if x=4 then y=40;else y=5000;put x= y=;

run;

Highlighted
## Re: why I am getting this answer?

Options

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

Posted in reply to Reeza

2 weeks ago

I totally agree, making it descriptive makes it easy to comprehend even by non technical audiences. My professor says that to me all the time and yet i lack attention to detail. Good one indeed!