BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Miah
Obsidian | Level 7

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

Pay attention to sas syntax:

 

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

    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

6 REPLIES 6
Community_Guide
SAS Moderator

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

.
Reeza
Super User

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

 

Miah
Obsidian | Level 7
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;

 

Reeza
Super User
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;
Shmuel
Garnet | Level 18

Pay attention to sas syntax:

 

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

    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; 

    

 

 

Astounding
PROC Star

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1215 views
  • 3 likes
  • 5 in conversation