BookmarkSubscribeRSS Feed
emaguin
Quartz | Level 8

Long time spss user. I want to see what the sas equivalent is for this.

 

do if (x eq 1 and y eq 3).

compute a=2.

compute b=3.

compute c=7.

else if (x eq 2 and y eq 4).

compute a=1.

compute b=0.

compute c=4.

else.

compute a=4.

compute b=5.

compute c=9.

end if.

 

or do if have to do this three times, subbing in b and c for a?

if (x eq 1 and y eq 3) then compute a=2;

else if (x eq 2 and y eq 4) then compute a=1;

else then compute a=4;  /* this line may not be correct */;

 

Thanks, Gene Maguin

 

 

 

 

4 REPLIES 4
ballardw
Super User

@emaguin wrote:

Long time spss user. I want to see what the sas equivalent is for this.

 

do if (x eq 1 and y eq 3).

compute a=2.

compute b=3.

compute c=7.

else if (x eq 2 and y eq 4).

compute a=1.

compute b=0.

compute c=4.

else.

compute a=4.

compute b=5.

compute c=9.

end if.

 

or do if have to do this three times, subbing in b and c for a?

if (x eq 1 and y eq 3) then compute a=2;

else if (x eq 2 and y eq 4) then compute a=1;

else then compute a=4;  /* this line may not be correct */;

 

Thanks, Gene Maguin

 

 

 

 


if (x eq 1 and y eq 3) then do;
   a=2;
   b=3;
   c=7;
end;
else if (x eq 2 and y eq 4) then do;
   a=1;
   b=0;
   c=4;
end;
else do;
   a=4;
   b=5;
   c=9;
end ;

Please post code or log entries into a code box opened using the forum's {I} icon to preserve formatting and to prevent the forum from inserting additional new line characters.

tomrvincent
Rhodochrosite | Level 12
data foo;
	x=1;
	y=3;

	if (x eq 1 and y eq 3) then
		do;
			a=2;
			b=3;
			c=7;
		end;
	else if (x eq 2 and y eq 4) then
		do;
			a=1;
			b=0;
			c=4;
		end;
	else
		do;
			a=4;
			b=5;
			c=9;
		end;
run;
PGStats
Opal | Level 21

I don't know about SPSS, by SAS syntax is fully described and explained in the documentation. You could start here, for example:

 

https://documentation.sas.com/?docsetId=basess&docsetTarget=p1n2fl39v0v2ffn1m3vm4a9kz2e5.htm&docsetV... 

PG
ChrisHemedinger
Community Manager

If a SELECT-WHEN (or CASE-WHEN in SQL) is more in line with your mental model, you can use SELECT-WHEN-OTHERWISE.

 

data in;
  x=1;  y=3;
  output;
  x=2;  y=4;
  output;
  x=0;  y=0;
  output;
run;

data foo;
  set in;
  select;
   when (x eq 1 and y eq 3) 
    do;
      a=2; b=3; c=7;
    end;
   when (x eq 2 and y eq 4)
    do;
      a=1; b=0; c=4;
    end;
  otherwise
    do;
      a=4; b=5; c=9;
    end;
  end;
run;
SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 4 replies
  • 1177 views
  • 2 likes
  • 5 in conversation