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 classified 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;
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;
Hello @Miah,
Your question requires more details before experts can help. Can you revise your question to include more information?
Review this checklist:
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.
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
.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
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;
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;
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;
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.