Do not use on any
shared computer
July 25, 2008 05:49am pdt
null
[x]
Attachment Details

How to create a SAS macro with arrays?

Zone: SAS
I am looking for a way to loop through 10 macro variables to create 10 separate datasets (first part ) and then within each dataset to loop for &MXCNT(created earlier) number of variables to calculate the differences between consecutive variables (second part).

The below codes shows the first part works, but in the second part blows up when trying to create arrays within %macro.

The error occurs at ...
"&&var&j1-&&var&j&MAXCNT".

SAS doesn't seem to recognize if i=1, then "&&var&j1" refers to a variable called EMPLOY1. I was trying to use %array and %do_over in the open codes to help with correcting the problem (a paper talked about these macro arrays http://www2.sas.com/proceedings/sugi31/040-31.pdf). However, SAS doesn't seem to recognize these two commands. See below error message for the simple example used in the paper. %array and %do_over look handy, do you know why they are not working?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
%let var1=EMPLOY;
%let var2=LEGAL;
%let var3=SOCIAL;
%let var4=SUBSTANCE;
%let var5=TECHNICAL;
%let var6=MENTAL;
%let var7=FAMILY;
%let var8=FINANCE;
%let var9=ASSAULT;
%let var10=MEDICAL;
%MACRO NEEDS;
%Do j = 1 %to 10;
      proc transpose data=NEEDSPOST0 out=&&var&j (drop=_name_ _label_) prefix=&&var&j;
        by DOC_ID;
        id count;
        var &&var&j;
        run;
      /*first part end*/
      data &&var&j;
      set &&var&j;
            array &&var&j{*} $15 &&var&j1-&&var&j&MAXCNT;
            array diff&&var&j{*} $15 diff&&var&j1-diff&&var&j&MXCNT;
            array negdiff&&var&j{*} $15
negdiff1&&var&j-negdiff&&var&j&MXCNT;
            array posdiff&&var&j{*} $15
posdiff1&&var&j-posdiff&&var&j&MXCNT;
            do i=1 to &MXCNT;
                  diff&&var&j{i} =&&var&j{i+1}-&&var&j{i};
                  if diff&&var&j{i}>0 then negdiff&&var&j{i}=1; else negdiff&&var&j{i}=0;
                  if i=1 then totnegdiff&&var&j=negdiff&&var&j{i};
                        else
totnegdiff&&var&j=totnegdiff&&var&j+negdiff&&var&j{i};
                  if diff&&var&j{i}<=0 and diff&&var&j{i}ne . then posdiff&&var&j{i}=1; else posdiff&&var&j{i}=0;
                  if i=1 then totposdiff&&var&j=posdiff&&var&j{i};
                        else
totposdiff&&var&j=totposdiff&&var&j+posdiff&&var&j{i};
                  drop i;
            end;
      run;
%END;
%MEND NEEDS;
 
 
 
140  %ARRAY(abc,VALUES=A B C);
     -
     180
WARNING: Apparent invocation of macro ARRAY not resolved.
 
ERROR 180-322: Statement is not valid or it is used out of proper order.
 
WARNING: Apparent invocation of macro ARRAY not resolved.
141  %ARRAY(def,VALUES=D E F);
     -
     180
 
ERROR 180-322: Statement is not valid or it is used out of proper order.
 
WARNING: Apparent invocation of macro DO_OVER not resolved.
142  Rename %DO_OVER(abc def,PHRASE=?abc=?def);
     ------
     180
 
ERROR 180-322: Statement is not valid or it is used out of proper order.
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Database
Question Asked By: M3hcSS
Solution Provided By: bradanelson
Participating Experts: 1
Solution Grade: A
Views: 33
Translate:
Loading Advertisement...
 
[+][-]Accepted Solution by bradanelson
Accepted Solution by bradanelson:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by bradanelson
Expert Comment by bradanelson:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080723-EE-VQP-34 / EE_QW_2_20070628