DATA Step, Macro, Functions and more

Arrays and Macro Variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

Arrays and Macro Variables

I am working with a transaction type data set where there are multiple observations for individual children.  Each child has a unique "child_id" and there are multiple observations, for which the child has an associated value for the variable TREATMENT.  There may or may not be repeats for the values of TREATMENT.  I would like to create a sequence of variables whereby I can see all of the values of the TREATMENTS that a child has received.  I have generated dummy variables based on all of the possible TREATMENT values with the following:


PROC SQL NOPRINT;

select distinct TREATMENT

into Treatment_List SEPARATED BY ' '

FROM treatments;

quit;

%PUT &treatment_list;


DATA LIST (DROP=I);

SET treatments;

ARRAY TEMP{*} &treatment_list;

DO I=1 TO DIM(TEMP);

TEMP{I}=0;

END;

All of the dummy variables, which correspond to the TREATMENT levels, currently have a zero.  However, I am unable to figure out how to code these dummy variables so that the treatments the children have received can be identified. 

Thank you for your help.


Accepted Solutions
Solution
‎10-23-2014 11:54 PM
Trusted Advisor
Posts: 1,204

Re: Arrays and Macro Variables

data treatments;
input Child Treatment $;
datalines;
1 A
1 B
1 C
2 B
2 C
3 A
3 C
3 D
;

PROC SQL NOPRINT;
select distinct TREATMENT
into :Treatment_List SEPARATED BY ' '
FROM treatments;
quit;

%PUT &treatment_list;

DATA LIST (DROP=I);
SET treatments;
ARRAY TEMP{*} &treatment_list;
DO I=1 TO DIM(TEMP);
if Treatment=vname(temp{i}) then TEMP{I}=1;
else TEMP{I}=0;
END;
run;

proc sql;
create table want as
select child,sum(A) as A,sum(B) as B,sum(c) as C,sum(D) as D
from list
group by child;
quit;

View solution in original post


All Replies
Super User
Posts: 17,831

Re: Arrays and Macro Variables

Sample input of what your data looks like and what you want your output to look like would be helpful.

Occasional Contributor
Posts: 14

Re: Arrays and Macro Variables

My current data:

ChildTreatment
1A
1B
1C
2B
2C
3A
3C
3D

What I want my data to look like:

ChildABCD
11110
20110
31011
Solution
‎10-23-2014 11:54 PM
Trusted Advisor
Posts: 1,204

Re: Arrays and Macro Variables

data treatments;
input Child Treatment $;
datalines;
1 A
1 B
1 C
2 B
2 C
3 A
3 C
3 D
;

PROC SQL NOPRINT;
select distinct TREATMENT
into :Treatment_List SEPARATED BY ' '
FROM treatments;
quit;

%PUT &treatment_list;

DATA LIST (DROP=I);
SET treatments;
ARRAY TEMP{*} &treatment_list;
DO I=1 TO DIM(TEMP);
if Treatment=vname(temp{i}) then TEMP{I}=1;
else TEMP{I}=0;
END;
run;

proc sql;
create table want as
select child,sum(A) as A,sum(B) as B,sum(c) as C,sum(D) as D
from list
group by child;
quit;

Super Contributor
Posts: 305

Re: Arrays and Macro Variables

Hello,

If you don't mind missing instead of 0 you can use transpose:

data have;
input Child Treatment $;
datalines;
1 A
1 B
1 C
2 B
2 C
3 A
3 C
3 D
;

data int;
retain x 1;
set have;
run;


proc transpose data=int out=want (drop=_NAME_);
by child;
id Treatment;
var x;
run;

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 227 views
  • 0 likes
  • 4 in conversation