DATA Step, Macro, Functions and more

IF Statement

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

IF Statement

[ Edited ]

I need to create a variable in my data set, indicating how many times each specimen was captured called NumCap. Then I need to alter the AgeGroup variable so that any specimen in the semi-adult group is classi fied as adult.

Finally I need to print the data set so that only the Sex, AgeGroup, Weight, and NumCap variables are displayed. 

 

PROC IMPORT OUT= work.capture
DATAFILE= "/folders/myfolders/Capture.txt"
DBMS=CSV REPLACE;
DELIMITER='09'x;
GETNAMES=YES;
RUN;

PROC PRINT DATA=work.capture;
RUN;

DATA capture;
SET capture;
NumCap = 0;
IF (Cap1 == 'yes') THEN DO;
NumCap +1;
IF (Cap2 == 'yes') THEN DO;
NumCap +1;
IF (Cap3 == 'yes') THEN DO;
NumCap +1;
IF (Cap4 == 'yes') THEN DO;
NumCap +1;
IF (Cap5 == 'yes') THEN DO;
NumCap +1;
IF (Cap6 == 'yes') THEN DO;
NumCap +1;
RUN;

DATA capture;
SET capture;
IF (AgeGroup == 'semi-adult') THEN DO;
AgeGroup = 'Adult';
RUN;

Accepted Solutions
Solution
‎03-04-2018 10:05 PM
Trusted Advisor
Posts: 1,831

Re: IF Statement

Pay attention to sas syntax:

 

1) DO; statement is used to do one or more statements, each ending by a semicolon (Smiley Wink.

    You have to enclose the DO; by END; statement as in:

do;
     <statement 1>;
     <statement 2>;
     ....
end;

2) When you have just one statement to do you don't need a DO; statement.

    You can use a more simple syntax like:

   

if <condition> then <statement>;

3) You can eclose the two missions into one data step.

 

4) Using above syntax, makes your code into:

  

DATA capture; 
	SET capture; 
	NumCap = 0;
	IF Cap1 = 'yes'  THEN NumCap +1; 
	IF Cap2 = 'yes'  THEN NumCap +1; 
	IF Cap3 = 'yes'  THEN NumCap +1; 
	IF Cap4 = 'yes'  THEN NumCap +1; 
	IF Cap5 = 'yes'  THEN NumCap +1; 
	IF Cap6 = 'yes'  THEN NumCap +1; 

       IF Agegroup = 'semi-adult' then Agegroup = 'Adult';
RUN;

5) Next code is eqivalent to the above:

    

DATA capture; 
	SET capture; 
	NumCap = 0;
        array cp Cap1-Cap5;
        do i=1 to 5;
            if cp(i) = 'yes' then numcap +1;
        end;

	IF agegroup = 'semi-adult' then agegroup = 'adult';
run; 

    

 

 

View solution in original post


All Replies
SAS Moderator
Posts: 55

Re: IF Statement [how to improve your question]

Hello @Miah,


Your question requires more details before experts can help. Can you revise your question to include more information? 

 

Review this checklist:

  • Specify a meaningful subject line for your topic.  Avoid generic subjects like "need help," "SAS query," or "urgent."
  • When appropriate, provide sample data in text or DATA step format.  See this article for one method you can use.
  • If you're encountering an error in SAS, include the SAS log or a screenshot of the error condition. Use the Photos button to include the image in your message.
    use_buttons.png
  • It also helps to include an example (table or picture) of the result that you're trying to achieve.

To edit your original message, select the "blue gear" icon at the top of the message and select Edit Message.  From there you can adjust the title and add more details to the body of the message.  Or, simply reply to this message with any additional information you can supply.

 

edit_post.png

SAS experts are eager to help -- help them by providing as much detail as you can.

 

This prewritten response was triggered for you by fellow SAS Support Communities member @Reeza

.
Super User
Posts: 23,296

Re: IF Statement

What have you tried so far? 

Showing what you've attempted makes it easier to help you. 

 

If you're not sure where to start, PROC MEANS calculates summary statistics and PROC FREQ generates summary counts. 

 

This section has a lot of tutorials and the first two programming e-courses are free.

http://video.sas.com/#category/videos/sas-analytics-u

 

Contributor
Posts: 22

Re: IF Statement

PROC IMPORT OUT= work.capture
DATAFILE= "/folders/myfolders/Capture.txt"
DBMS=CSV REPLACE;
DELIMITER='09'x;
GETNAMES=YES;
RUN;

PROC PRINT DATA=work.capture;
RUN;

DATA capture;
SET capture;
NumCap = 0;
IF (Cap1 == 'yes') THEN DO;
NumCap +1;
IF (Cap2 == 'yes') THEN DO;
NumCap +1;
IF (Cap3 == 'yes') THEN DO;
NumCap +1;
IF (Cap4 == 'yes') THEN DO;
NumCap +1;
IF (Cap5 == 'yes') THEN DO;
NumCap +1;
IF (Cap6 == 'yes') THEN DO;
NumCap +1;
RUN;

DATA capture;
SET capture;
IF (AgeGroup == 'semi-adult') THEN DO;
AgeGroup = 'Adult';
RUN;

 

Super User
Posts: 23,296

Re: IF Statement

DATA capture; 
SET capture;

Coding like that makes it really hard to figure out where you made a mistake, make sure each DATA creates a unique data set instead.

 

 SAS does not use a double == to test equality, a single equal sign is necessary. How does this code not work or meet your expectations? What are you expecting as output?

 

If you have an THEN DO block,  it needs an END.

 

	IF (AgeGroup == 'semi-adult') THEN DO;
	AgeGroup = 'Adult';
        END;
Solution
‎03-04-2018 10:05 PM
Trusted Advisor
Posts: 1,831

Re: IF Statement

Pay attention to sas syntax:

 

1) DO; statement is used to do one or more statements, each ending by a semicolon (Smiley Wink.

    You have to enclose the DO; by END; statement as in:

do;
     <statement 1>;
     <statement 2>;
     ....
end;

2) When you have just one statement to do you don't need a DO; statement.

    You can use a more simple syntax like:

   

if <condition> then <statement>;

3) You can eclose the two missions into one data step.

 

4) Using above syntax, makes your code into:

  

DATA capture; 
	SET capture; 
	NumCap = 0;
	IF Cap1 = 'yes'  THEN NumCap +1; 
	IF Cap2 = 'yes'  THEN NumCap +1; 
	IF Cap3 = 'yes'  THEN NumCap +1; 
	IF Cap4 = 'yes'  THEN NumCap +1; 
	IF Cap5 = 'yes'  THEN NumCap +1; 
	IF Cap6 = 'yes'  THEN NumCap +1; 

       IF Agegroup = 'semi-adult' then Agegroup = 'Adult';
RUN;

5) Next code is eqivalent to the above:

    

DATA capture; 
	SET capture; 
	NumCap = 0;
        array cp Cap1-Cap5;
        do i=1 to 5;
            if cp(i) = 'yes' then numcap +1;
        end;

	IF agegroup = 'semi-adult' then agegroup = 'adult';
run; 

    

 

 

Super User
Posts: 6,632

Re: IF Statement

A few changes to the basic syntax will do the trick.

 

First, if you want to do something, just do it.  You don't have to say THEN DO

 

Second, you only need one equal sign to assign a value.  

 

The result:

 

data want;

   set capture;

   NumCap = 0;

   if Cap1='yes' then NumCap + 1;

   if Cap2='yes' then NumCap + 1;

   if Cap3='yes' then Numcap + 1;

   if Cap4='yes' then NumCap + 1;

   if Cap5='yes' then NumCap + 1;

   if Cap6='yes' then NumCap + 1;

   if AgeGroup = 'semi-adult' then AgeGroup = 'Adult';

run;

 

There are ways to shorten the amount of code, using an array to hold Cap1 through Cap6.  I omitted that intentionally, since it is more complex and you are still working on some more basic statements.  Just in case it proves useful, these statements could replace 6 IF/THEN statements:

 

array cap {6};

do k=1 to 6;

   if cap{k} = 'yes' then NumCap + 1;

end;

drop k;

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 194 views
  • 3 likes
  • 5 in conversation