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

Hi everybody

My dataset i organized like this spreadsheet. I have repeated measeurements for individuals with the ID 1-2 at time 1-6 and three dichotomous variables a-c. Variable B is only measured at time 1-3 and variable C is only measured at time 4-6. I want to make a variable with the number of 'yes' for variable B (example: V_t1-3) and a variable with the number of 'yes' for variable C (example:V_t4-6). I would prefer if these new variables were only listed once per ID, for example as I have done with V_t1-3 and V_t4_6 at time_1.

Any suggestions

I am a SAS novice using SAS 9.3

Sincerely

Anders

IDTimeVariable AVariable BVariable CV_t1-3V_t4-6
11no02
12no
13no
14yes
15no
16yes
21yes11
22no
23no
24no
25no
26yes
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

How about:

Assuming every ID group has time 1.

data have;
input ID     Time     VariableA $     VariableB $     VariableC     $;
cards;
1     1     .     no     .
1     2     .     no     .          
1     3     .     no     .          
1     4     .     .     yes          
1     5     .     .     no          
1     6     .     .     yes          
2     1     .     yes      .
2     2     .     no     .          
2     3     .     no     .          
2     4     .     .     no          
2     5     .     .     no          
2     6     .     .     yes
;
run;
proc sql ;
create table want(drop=_:) as
 select *
  from have left join (select id as _id ,1 as _time ,sum(VariableB='yes') as V_t1_3,sum(VariableC='yes') as V_t4_6 from have group by id)
   on id=_id and time=_time;
quit;


Ksharp

View solution in original post

6 REPLIES 6
Ksharp
Super User

How about:

Assuming every ID group has time 1.

data have;
input ID     Time     VariableA $     VariableB $     VariableC     $;
cards;
1     1     .     no     .
1     2     .     no     .          
1     3     .     no     .          
1     4     .     .     yes          
1     5     .     .     no          
1     6     .     .     yes          
2     1     .     yes      .
2     2     .     no     .          
2     3     .     no     .          
2     4     .     .     no          
2     5     .     .     no          
2     6     .     .     yes
;
run;
proc sql ;
create table want(drop=_:) as
 select *
  from have left join (select id as _id ,1 as _time ,sum(VariableB='yes') as V_t1_3,sum(VariableC='yes') as V_t4_6 from have group by id)
   on id=_id and time=_time;
quit;


Ksharp

loedrup_ab
Calcite | Level 5

Hi Ksharp

That was amazing. I worked perfectly. Thank you very much.Smiley Happy

If you want to you, I would be very interested in learning the meaning of the different parts of your proc sql code. But only if you feel like it of course.

SIncerely

Anders

Ksharp
Super User

Of course.

(select id as _id ,1 as _time ,sum(VariableB='yes') as V_t1_3,sum(VariableC='yes') as V_t4_6 from have group by id)

this statement creat a table which contains V_t1_3 and V_t4_6 for each ID group ,then I use LEFT JOIN to bring it into the original dataset . That is it , EASY ??

Ksharp

loedrup_ab
Calcite | Level 5

Thanks again Ksahrp

You have been really helpfull.

Take care.

SIncerely

Anders

venkateshn
Calcite | Level 5

hi

how we get  v_t1_3 and v_t4_6 values here ...........

thanks

Linlin
Lapis Lazuli | Level 10

Hi,

run the code below may help you to understand:

data have;
input ID     Time     VariableA $     VariableB $     VariableC     $;
cards;
1     1     .     no     .
1     2     .     no     .         
1     3     .     no     .         
1     4     .     .     yes         
1     5     .     .     no         
1     6     .     .     yes         
2     1     .     yes      .
2     2     .     no     .         
2     3     .     no     .         
2     4     .     .     no         
2     5     .     .     no         
2     6     .     .     yes
;
run;

proc sql;
  select  id as _id ,1 as _time,sum(VariableB='yes') as V_t1_3,sum(VariableC='yes') as V_t4_6 from have group by id;
quit;

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 972 views
  • 7 likes
  • 4 in conversation