Hi! I create a dataset with a variable ID from 1 to 22. I want to give a new variable called "twins" the value 1 for just object 20 (all other objekts I do want to have the value 0 for that new variable) if both objects with ID 21 and 22 is on the file othervise 0. How do I do that?
I'm not understanding. I thought I understood this part: "I create a dataset with a variable ID from 1 to 22. I want to give a new variable called "twins" the value 1 for just object 20 (all other objekts I do want to have the value 0 for that new variable)" but then this part confused me totally: "...if both objects with ID 21 and 22 is on the file othervise 0."
Please show us the original data set, and the desired result.
I still don't understand.
Please show us the input data set and the desired output.
And do NOT post data in pictures. Post data as text, ideally as a DATA step with DATALINES which recreates your dataset successfully when submitted.
I think you're mixing up two variables which is what is making this confusing.
Here's my interpretation of your problem:
*data of random objects you do have;
data have;
do object=1 to 25;
output;
end;
run;
*randomly select 22;
proc surveyselect data=have out=selected method=srs sampsize=22 seed=245;
run;
*add in an id for each row;
data selected;
set selected;
id=_n_;
run;
*check if you have the 21 and 22 observations;
%let countobs=0;
proc sql;
create table temp as
select *
from selected where object in (21, 22);
quit;
%let countobs=&sqlobs;
*if observations are present add the flag;
data want;
set selected;
twins=0;
if id=20 and &countobs=2 then twins=1;
run;
If this is incorrect, please show sample input data and expected output, similar to what I have posted above.
Not only do your pictures not appear, you have ignored what you were told earlier by @Kurt_Bremser : "do NOT post data in pictures. Post data as text, ideally as a DATA step with DATALINES which recreates your dataset successfully when submitted." We'd like to help you, but you have to help us too.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.