Help using Base SAS procedures

make new variable from repeated measurements?

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

make new variable from repeated measurements?

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

Accepted Solutions
Solution
‎05-02-2012 04:18 AM
Super User
Posts: 9,681

Re: make new variable from repeated measurements?

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


All Replies
Solution
‎05-02-2012 04:18 AM
Super User
Posts: 9,681

Re: make new variable from repeated measurements?

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

Contributor
Posts: 28

Re: make new variable from repeated measurements?

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

Super User
Posts: 9,681

Re: make new variable from repeated measurements?

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

Contributor
Posts: 28

Re: make new variable from repeated measurements?

Thanks again Ksahrp

You have been really helpfull.

Take care.

SIncerely

Anders

Occasional Contributor
Posts: 5

Re: make new variable from repeated measurements?

hi

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

thanks

Super Contributor
Posts: 1,636

Re: make new variable from repeated measurements?

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;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 231 views
  • 6 likes
  • 4 in conversation