DATA Step, Macro, Functions and more

Problem in Macro Loops , Macro Scope

Reply
N/A
Posts: 0

Problem in Macro Loops , Macro Scope

%macro test1;

data libcan.q9;
set libcan.hoppalason;

%do i=1 %to 10;
%global &&skodf&i;
%global &&char2f&i;
%global &&char3f&i;
/*%global &&ilkdonemf&i;*/
%end;

%let ncan=10;

%do i=1 %to 10;

if _n_=&i then call symputx("skodf&i",skod);
if _n_=&i then call symputx("char2f&i",char2);
if _n_=&i then call symputx("char3f&i",char3);
/*if _n_=&i then call symputx("ilkdonem&i",ilkdonem);*/

%end;


%do i=1 %to &ncan;
%do j=1 %to &ncan;

%if ("&&skodf&i" = "&&skodf&j") and ("&&char2f&i" = "&&char2f&j") and ("&&char3f&i" = "&&char3f&j")
%then %do;
data libcan.aa&j;
set libcan.hoppala;
output1 = "LINE EXTENTION";
if _n_=&j;
run;
%end;
%else %if ("&&skodf&i" = "&&skodf&j") and ("&&char2f&i" = "&&char2f&j") and ("&&char3f&i" NE "&&char3f&j")
%then %do;
data libcan.aa&j;
set libcan.hoppala;
output2 = "BRAND EXTENSION";
if _n_=&j;
run;
%end;
%else %if ("&&skodf&i" NE "&&skodf&j") and ("&&char2f&i" = "&&char2f&j") and ("&&char3f&i" = "&&char3f&j")
%then %do;
data libcan.aa&j;
set libcan.hoppala;
output3 = "NEW CATEGORY TO BRAND";
if _n_=&j;
run;
%end;
%else %if ("&&skodf&i" NE "&&skodf&j") and ("&&char2f&i" = "&&char2f&j") and ("&&char3f&i" NE "&&char3f&j")
%then %do;
data libcan.aa&j;
set libcan.hoppala;
output4 = "NEW BRAND";
if _n_=&j;
run;
%end;
%else %if ("&&skodf&i" = "&&skodf&j") and ("&&char2f&i" NE "&&char2f&j") and ("&&char3f&i" NE "&&char3f&j")
%then %do;
data libcan.aa&j;
set libcan.hoppala;
output5 = "BLANK1";
if _n_=&j;
run;
%end;
%else %if ("&&skodf&i" NE "&&skodf&j") and ("&&char2f&i" NE "&&char2f&j") and ("&&char3f&i" NE "&&char3f&j")
%then %do;
data libcan.aa&j;
set libcan.hoppala;
output6 = "BLANK2";
if _n_=&j;
run;
%end;
%else %if ("&&skodf&i" NE "&&skodf&j") and ("&&char2f&i" NE "&&char2f&j") and ("&&char3f&i" = "&&char3f&j")
%then %do;
data libcan.aa&j;
set libcan.hoppala;
output7 = "BLANK3";
if _n_=&j;
run;
%end;
%else %if ("&&skodf&i" = "&&skodf&j") and ("&&char2f&i" NE "&&char2f&j") and ("&&char3f&i" = "&&char3f&j")
%then %do;
data libcan.aa&j;
set libcan.hoppala;
output8 = "BLANK4";
if _n_=&j;
run;
%end;
%end;

%end;

run;

data libcan.kategorize;
set
%do k=1 %to 10;
libcan.aa&k
%end;
;
run;
%mend;



options mprint;
options symbolgen;
options macrogen;
%test1;



I have problem with my macro function. First problem is:
i get this: ERROR: Invalid symbolic variable name &
and ERROR: Invalid symbolic variable name &
errors.
My aim is to categorize my dataset with some conditions above.
But i can't because of these errors.
Also i write my loop as i=1 and j=1 and suppose that my dataset look like:
A B C
D E F
A C D

SO; for i=1 and j=1 it should return LINE EXTENTION where it don't, it says it is BRAND EXTENSION.
If someone understand where i am and try what to do, please help me.
I am in trouble.
Regular Contributor
Posts: 171

Re: Problem in Macro Loops , Macro Scope

Posted in reply to deleted_user
You need to remove the two leading ampersands from the three global statements in the beginning of the macro. For example, when &i resolves to 1, the statement %global &&skodf&i; will resolve to %global &skodf1;, which produces an error because a macro variable name cannot include an ampersand. This explains the error messages you referenced. I suspect, however, that there may be a couple of additional problems with this code that you will still need to resolve.
Regular Contributor
Posts: 241

Re: Problem in Macro Loops , Macro Scope

Posted in reply to deleted_user
@cakcan: I honestly don't know what you are trying to do. I get the vague sense that you are trying to compare an observation against another. See if below makes any sense to you.

The output has all the combination of each observation (whose obs number indicated by i) compared against another (j), including itself. Given the pair of observations, then each of the three variables are compared: first var of the ith obs compared against the first var of the jth obs, and so on.

The results of the comparisons are simply summarized by the code variable, where 1 means match and 0 means no-match. For instance, the code value of 100 indicates that the first variable value of the ith observation matched with the first variable value of the jth observation, but the second and the third variable values did not. Then using a format, the code is translated into type. Hope this helps a bit.
[pre]

/* test data */
data one;
input i (i1 i2 i3) (:$1.);
cards;
1 A B C
2 A B D
3 X B C
4 X B Y
;
run;

/* full join by itself */
data two;
set one nobs=nobs;
do point = 1 to nobs;
set one(rename=(i=j i1=j1 i2=j2 i3=j3)) point=point;
output;
end;
run;

/* generate type */
proc format;
value $type
"111" = "line extention"
"110" = "brand extension"
"011" = "new category to brand"
"010" = "new brand"
other = "blank";
run;
data three;
set two;
code = cats(i1=j1, i2=j2, i3=j3);
type = put(code,$type21.);
run;

/* check */
proc print data=three noobs;
var i j code type;
run;
/* on lst
i j code type

1 1 111 line extention
1 2 110 brand extension
1 3 011 new category to brand
1 4 010 new brand
2 1 110 brand extension
2 2 111 line extention
2 3 010 new brand
2 4 010 new brand
3 1 011 new category to brand
3 2 010 new brand
3 3 111 line extention
3 4 110 brand extension
4 1 010 new brand
4 2 010 new brand
4 3 110 brand extension
4 4 111 line extention
*/
[/pre]
SAS Super FREQ
Posts: 8,868

Re: Problem in Macro Loops , Macro Scope

Posted in reply to deleted_user
Hi:
In addition to simplifying the logic and processing and deciding whether or not you need such a complicated program as the one you originally posted, As you see from the alternate solution that's been posted already, it is possible to solve some problems using non-macro approaches.

When and if you do need to use multiple ampersands in a solution, you might also want to read some of the following material on using macro variables and indirect referencing of macro variables (what you have when you use multiple ampersands in your code).

cynthia

http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a001071915.htm

http://www2.sas.com/proceedings/sugi30/165-30.pdf
http://www2.sas.com/proceedings/sugi27/p020-27.pdf
http://www2.sas.com/proceedings/sugi22/ADVTUTOR/PAPER44.PDF
N/A
Posts: 0

Re: Problem in Macro Loops , Macro Scope

Posted in reply to deleted_user
Thank you polingjw.
The error problem is solved by removing ampersands.
N/A
Posts: 0

Re: Problem in Macro Loops , Macro Scope

Posted in reply to deleted_user
But the other problem still occurs.
I'll try to explain my point of view. I have a large dataset and my aim is to categorize all rows to some conditions. For example my dataset looks like this:
SKOD ILKDONEM CHAR2 CHAR3
01 9509 PG KROMASH
01 9805 OTHER SUPMAX
01 9807 PG PERMATIK
02 9509 ASTOR ASTOR
03 9709 PERSON PERSONN
03 9906 OTHER SUPMAX
.. .. .. ..
.. .. .. ..

So my categorization criteria depends on 4 conditions:
1. LINE EXTENSION
("&&skodf&i" = "&&skodf&j") and ("&&char2f&i" = "&&char2f&j") and ("&&char3f&i" = "&&char3f&j")
2. BRAD EXTENSION
("&&skodf&i" = "&&skodf&j") and ("&&char2f&i" = "&&char2f&j") and ("&&char3f&i" NE "&&char3f&j")
3. NEW CATEGORY TO BRAND
("&&skodf&i" NE "&&skodf&j") and ("&&char2f&i" = "&&char2f&j") and ("&&char3f&i" = "&&char3f&j")
4. NEW BRAND
("&&skodf&i" NE "&&skodf&j") and ("&&char2f&i" = "&&char2f&j") and ("&&char3f&i" NE "&&char3f&j")

So, every row in the dataset should be categorized by these categories.
So for my loop starts as:
i=1 j=1
my first row of my dataset is equal to each other so it should output as:
LINE EXTENSION and also create a new dataset for each every row and finally, at the end it should merge all the output datasets togather so that i got to my point.
But my macro doesnt work like i think. It gives BRAND EXTENSION to my first row, and so on goes wrong like this. I don't understand why.
I don't want to change the algorithm (using macro), if someone have an idea where i'm wrong, please correct me. It would be great for me.
Frequent Contributor
Posts: 106

Re: Problem in Macro Loops , Macro Scope

Posted in reply to deleted_user
I'd say: lean back, take a deep breath and seriously look at Chang's suggestion. The code he provided seems perfectly suited to the task as you outlined it in the first post of this thread.

If the code (especially macro code) gets too complicated that's very often a "heads-up" like indicator to rethink the strategy.
N/A
Posts: 0

Re: Problem in Macro Loops , Macro Scope

Posted in reply to deleted_user
I've fixed the problem, it's just because merging the output datasets and overwriting problem. Just created a unique macro variable for incrementing the output sas tables differing from the loop variables (i,j) and it just worked.

Thank you guys,
Can Akcan
Ask a Question
Discussion stats
  • 7 replies
  • 744 views
  • 0 likes
  • 5 in conversation