DATA Step, Macro, Functions and more

If statement in SAS Proc

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

If statement in SAS Proc

Hi everyone,


I have two question please answer them:


1. Is it true that we can't use if statement in all SAS Procedures? Are there some procedures in which we can use if statement, If yes then please name some of them.

 

2. Can we use if statement in Proc sql? If yes, then please give me a small example.

 

Thanks,

Ramanuj


Accepted Solutions
Solution
‎04-08-2016 12:59 PM
Super User
Posts: 17,963

Re: If statement in SAS Proc

No you can't use IF statements in all procs.

 

I think you're intending to refer to WHERE statements. 

AFAIK you can use it all procs.

 

WHERE is a clause available in SQL.

 

You cannot use if statement in proc sql.  

View solution in original post


All Replies
Solution
‎04-08-2016 12:59 PM
Super User
Posts: 17,963

Re: If statement in SAS Proc

No you can't use IF statements in all procs.

 

I think you're intending to refer to WHERE statements. 

AFAIK you can use it all procs.

 

WHERE is a clause available in SQL.

 

You cannot use if statement in proc sql.  

Trusted Advisor
Posts: 1,115

Re: If statement in SAS Proc

Hi @Ramanuj_Khatri,

 

Answers:

  1. Examples of procedures which allow the use of an IF statement (in a certain way) include PROC PHREG, PROC GENMOD, PROC NLMIXED, PROC MCMC, PROC GLIMMIX and some other statistical procedures as well as PROC FCMP.
  2. No, an IF statement is not valid in PROC SQL. If you want to perform, e.g., conditional assignments in PROC SQL, please provide details on your problem.
Super User
Super User
Posts: 6,502

Re: If statement in SAS Proc

WHERE statements should work in all procs.  The main exception is PROC SQL which has its own WHERE clause as part of statements like SELECT, DELETE or UPDATE.

 

An actual IF/THEN statement is general NOT available in PROCS.  There are some procs that allow you to provide snippets of code that will support IF/THEN statements.

 

In PROC SQL the type of logic you would use in an IF/THEN statement you would normally code using CASE instead.

So in a data step you could write:

if sex= 1 then gender='Female';
else if sex=2 then gender='Male';
else gender='Unknown;

In PROC SQL you would write:

case when sex=1 then 'Male'
     when sex=2 then 'Female'
     else 'Unknown'
end as Gender
Contributor
Posts: 22

Re: If statement in SAS Proc

Thanks Tom, for detailed answer.
Super User
Posts: 10,552

Re: If statement in SAS Proc

You can use IF in Proc FCMP (which makes functions calleable in a datastep), Proc Template (in defining some options inside diferent types of templates) , In compute blocks in Proc Report.

I'm sure there are other places but I don't have every SAS module available.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 341 views
  • 4 likes
  • 5 in conversation