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;
Check out SAS Innovate on-demand content! Watch the main stage sessions, keynotes, and over 20 technical breakout sessions!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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