BookmarkSubscribeRSS Feed
SAS-programmer
Calcite | Level 5

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?

 

7 REPLIES 7
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
SAS-programmer
Calcite | Level 5
Hi!
I randomly create this type of file by chosing 5 objects of 22.
[cid:image001.png@01D8EAF6.31F98480]
I want to add a new row with ID=20 and in the case I recieve both number 21 and 22 I want to give a new variable the value 1 for ID=20 otherwise 0.
PaigeMiller
Diamond | Level 26

I still don't understand.

 

Please show us the input data set and the desired output.

--
Paige Miller
Kurt_Bremser
Super User

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.

Reeza
Super User

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.

SAS-programmer
Calcite | Level 5
My dataset Population looks like this:
[cid:image003.png@01D8EB95.9119C7F0]
And one example of my repeated samples drawn from Population looks like this and the dataset is called Sample:
[cid:image002.png@01D8EB94.BE8CB810]
Now I want in a third dataset Counter count two things. First how many times a certain object is drawn. And also how many times both ID 21 and 22 is both drawn.
In this case I want
PaigeMiller
Diamond | Level 26

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.

 

 

--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 460 views
  • 0 likes
  • 4 in conversation