DATA Step, Macro, Functions and more

macro array for summing the letter of codes

Reply
Regular Contributor
Regular Contributor
Posts: 170

macro array for summing the letter of codes

Hello all!

Hopefully someone can help me debug this program.

Right below is the starting mock table that outlines the problem...
(The tables don't render properly)
Patient_Number Day1 Day2 Day 3
123 A B A
124 A B C

The letters represent codes that have to be converted into their own variables and then summed so that what you have is:

Patient_Number Day1 Day2 Day 3 A B C
123 A B A 2 1 0
124 A B C 1 1 1

The first part of the macro creates the list and then there is an indirect call to it.
The second part attempts to create an array for every code to accomplish the above. The codes are part of a dataset...

the macro almost works as it does pass each code through. Except I am running into a hitch at the last moment. The problem is coming down to an improper "when" and/or improper do blocks somewhere.

The code and then part of the log follows below. Any assistance would be greatly appreciated!

THIS PART WORKS!
%macro split (data=, var=);
proc sort data=&data(keep=&var) out=values nodupkey;
by &var;
run;
data _null_;
set values end=last;
call symputx('site'||left(_n_),&var);
if last then call symputx('count',_n_);
run;
THIS PART DOES NOT!
DATA ALLDATA;
MERGE PTS INPUTDATA;
BY MR_;
select(&var);
%do i=1 %to &count;
when("&&site&i") %do;
array Cod&&site&i [7] Day1-Day7;
Code&&site&i=0;
DO I=1 to 7;
If Cod&&site&i="&&site&i" then Code&&site&i=Code&&site&i+1;
end;
%END;

%END;
drop I;
run;


%mend split;
%split(data=Codes, var=code)


Program Log:

DATA ALLDATA;
MPRINT(SPLIT): MERGE PTS INPUTDATA;
MPRINT(SPLIT): BY MR_;
MPRINT(SPLIT): select(code);
NOTE: Line generated by the invoked macro "SPLIT".
5 array Cod&&site&i [7] Day1-Day7; Code&&site&i=0; DO I=1 to 7; If
-----
180
5 ! Cod&&site&i="&&site&i" then Code&&site&i=Code&&site&i+1; end;
MPRINT(SPLIT): when("A") array CodA [7] Day1-Day7;
MPRINT(SPLIT): CodeA=0;
ERROR: Expecting "WHEN", "OTHERWISE", or "END".
MPRINT(SPLIT): DO I=1 to 7;
MPRINT(SPLIT): If CodA="A" then CodeA=CodeA+1;
MPRINT(SPLIT): end;
Super Contributor
Super Contributor
Posts: 3,174

Re: macro array for summing the letter of codes

Your %DO needs to be a DATA step DO statement -- to go with your SELECT/WHEN/END/OTHERWISE/END paragraph construct.

Scott Barry
SBBWorks, Inc.
Regular Contributor
Regular Contributor
Posts: 170

Re: macro array for summing the letter of codes

Scott;
Thanks for the tip! Got me one inch closer. Now it seems to run through but I get the errors:
NOTE: Character values have been converted to numeric values at the places given by:
(Line)Smiley SadColumn).
1:1 1:1 1:1 1:1 1:1 1:1 1:1 1:1 1:1 1:1 1:1 1:1 1:1 1:1 1:1
1:1 1:1 1:1 1:1 1:1 1:1 1:1 1:1 1:1 1:1 1:1 1:1 1:1 1:1 1:1
1:1 1:1 1:1 1:1 1:1 1:1 1:1 1:1 1:1
NOTE: Variable code is uninitialized.
NOTE: Invalid numeric data, 'A' , at line 1 column 1.

So it works for the first code but then stops...still playing with it.

Lawrence
Super Contributor
Super Contributor
Posts: 3,174

Re: macro array for summing the letter of codes

Suggest you have a look at PROC TRANSPOSE, instead.

Scott Barry
SBBWorks, Inc.
Regular Contributor
Regular Contributor
Posts: 170

Re: macro array for summing the letter of codes

A less elegent solution but doable. Just very close with this. Thank you for the suggestion...
SAS Super FREQ
Posts: 8,743

Re: macro array for summing the letter of codes

Hi:
Regarding this:
(The tables don't render properly)


when you want to show code or the SAS log or a mock table where it would be swell if everything lined up, like this:
[pre]
Patient_Number Day1 Day2 Day3
123 A B A
124 A B C
[/pre]

All you need to do is surround your code and/or your table with the PRE tags. Only these are not HTML <PRE> tags, the tag is
squarebracket-PRE-squarebracket_close
squarebracket-/PRE-squarebracket_close or

(PRE) and (/PRE)

except where I have the (, you would put [ and where I have the ), you would put ] -- that will tell the forum text editor and posting manager to NOT format anything within these special PRE tags.

cynthia
Regular Contributor
Regular Contributor
Posts: 170

Re: macro array for summing the letter of codes

Thanks for how-to...I am brand spanking new to the forum wiull abide by this in the future...
~LB
Regular Contributor
Regular Contributor
Posts: 170

Re: macro array for summing the letter of codes

I would like to thank Scott again for his help. Figured out the last part and now it works!
Ask a Question
Discussion stats
  • 7 replies
  • 172 views
  • 0 likes
  • 3 in conversation