debug help

Accepted Solution Solved
Reply
Regular Contributor
Posts: 190
Accepted Solution

debug help

[ Edited ]
hello, here below is my code---->
 
%macro grp_code_gen(groupref_id=,source_var1=);
65
66 data abc;
67 set work.gexcel11;
68 length statement $1000. end_val start_val $5. source $10.;
69 source=&source_var1;
70 grp_ref=&groupref_id;
71 start_val=put(source_start,5.);
72 end_val=put(source_end,5.);
73 if source=&source_var1 and grp_ref=&groupref_id then do;
74 statement= "IF" ||&source_var1||">"| |"="||(start_val)||"<"||'AND'||(end_val)||" THEN
74 ! "||target_var_name||"="||target_value;
75 put ' ' statement ;
76 end;
77 run;
78
79 %mend grp_code_gen;
80 %grp_code_gen(groupref_id=G002,source_var1='-ageY');
 
NOTE: Variable G002 is uninitialized.
IF-ageY>= 65<AND 80 THEN AGEGR1=>=65, < 80
IF-ageY>= .<AND 65 THEN AGEGR1=< 65
IF-ageY>= 65<AND . THEN AGEGR1=> =80
 
Recomend changes to my IF 'statement' how can i remove the periods '.' from the output  & display the following output. thanks in advance.
 
IF AGEX >= 65 AND AGEX < 80 THEN AGEGR1 = '>=65, < 80 '
IF AGEX < 65 THEN AGEGR1 = '> 65 '
IF AGEX > 80 THEN AGEGR1 = '< 80 '

Accepted Solutions
Solution
‎01-18-2016 01:10 AM
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: debug help

An example, plain base SAS code:

data gexcel11;
  infile datalines dlm="|" missover;
  length command1 command2 target_value $2000;
  input command1 $ command2 $ target_value $;
datalines;
AGEY >= 65|AGEY < 80|>=65, < 80
AGEY < 65| |< 65
AGEY >= 65| |> =80
;
run;

data _null_;
  set gexcel11;
  length command $2000;
  command=cat("if ",catx(' and ',command1,command2)," then agegr1='",strip(target_value),"';");
  put command;
run;

View solution in original post


All Replies
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: debug help

Put in additional conditions:

if source=&source_var1 and grp_ref=&groupref_id then do;
/* Here */
  if start_val ne . then 
  statement= "IF" ||&source_var1||">"| |"="||(start_val)||" "||'AND'||(end_val)||" THEN "||target_var_name||"="||target_value;
  else statement= "IF" ||&source_var1||"<"|||(end_val)||" THEN "||target_var_name||"="||target_value;
  put ' ' statement ;
end;

I would suggest you look at your metadata.  If you structure it correctly, then your coding will become a lot simpler, e.g. from:

COMMAND1                COMMAND2     TARGET_VALUE

AGEX >= 65                 AGEX < 80       >=65, <80

AGEX < 65                                         <65

..

You can then do. 

 

  command=cat(catx('and',command1,command2)," then ",target_name,"='",target_value,"'");

Simplest way is to write a basic SAS datastep program to do what you need to do.  Then start to identify patterns in the code, bits that can be replaced.  Follow SLDC, design, document, code, test, release, cycle.  Your trying to work backwards through a layer of obfuscation, for example, no macro is needed here, a simple data _null_ and call execute will do the trick and be simpler.

Regular Contributor
Posts: 190

Re: debug help

thanks for d inputs
63
64 %macro grp_code_gen(source_var1=);
65 data _null_;
66 set work.gexcel11;
67 length statement $1000. end_val start_val $5. source $10.;
68 source= &source_var1;
69 start_val=put(source_start,5.);
70 end_val=put(source_end,5.);
71 if source=&source_var1 then do;
72 if start_val ne . then
73 statement= "IF" ||&source_var1||">"| |"="||(start_val)||" "||'AND'||(end_val)||" THEN
73 ! "||target_var_name||"'"||strip(target_value)||"'";
74 else statement= "IF" ||&source_var1||"<"|| (end_val)||" THEN "||target_var_name||"'"||strip(target_value)||"'";
75 put ' ' statement ;
76 end;
77 run;
78
79 %mend grp_code_gen;
80 %grp_code_gen(source_var1='ageY');

NOTE: Character values have been converted to numeric values at the places given by: (Line)Smiley SadColumn).
80:219
IFageY>= 65 AND 80 THEN AGEGR1'>=65, < 80'
IFageY< 65 THEN AGEGR1'< 65'
IFageY>= 65 AND . THEN AGEGR1'> =80'
but 1 condition is missing--> IF AGEX < 65 THEN AGEGR1 = '> 65 ' in the 2 row @RW9
Regular Contributor
Posts: 190

Re: debug help

[ Edited ]

IFageY< 65 THEN AGEGR1='> 65' this condition isn't getting applied &displayed....Smiley Sad

Esteemed Advisor
Posts: 6,646

Re: debug help

64 %macro grp_code_gen(source_var1=);
65 data _null_;
66 set work.gexcel11;
67 length statement $1000. end_val start_val $5. source $10.;
68 source= &source_var1;
69 start_val=put(source_start,5.);
70 end_val=put(source_end,5.);
71 if source=&source_var1 then do; This condition will always be true, as per line 68!
72 if start_val ne . then
73 statement= "IF" ||&source_var1||">"| |"="||(start_val)||" "||'AND'||(end_val)||" THEN
73 ! "||target_var_name||"'"||strip(target_value)||"'";
74 else statement= "IF" ||&source_var1||"<"|| (end_val)||" THEN "||target_var_name||"'"||strip(target_value)||"'";
75 put ' ' statement ;
76 end;
77 run;
78
79 %mend grp_code_gen;

 

In order to debug this, you need to supply the contents of work.gexcel11.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 190

Re: debug help

[ Edited ]

this the output i need

IF AGEX >= 65 AND AGEX < 80 THEN AGEGR1 = '>=65, < 80 '
IF AGEX < 65 THEN AGEGR1 = '> 65 '
IF AGEX > 80 THEN AGEGR1 = '< 80 '

but my output is--->

IFageY>= 65 AND 80 THEN AGEGR1'>=65, <80'
IFageY< 65 THEN AGEGR1'< 65'
IFageY>= 65 AND . THEN AGEGR1'> =80'

 

how do i update d code to display d above o/p.

Grand Advisor
Posts: 17,333

Re: debug help

Please post the code and log separately, with the options mprint and symbolgen turned on so you/we can see what code is being generated.

Regular Contributor
Posts: 190

Re: debug help

@Reeza the code & log as follows--->

%macro grp_code_gen(source_var1=);
data _null_;
set work.gexcel11;
length statement $1000. end_val start_val $5. source $10.;
source= &source_var1;
start_val=put(source_start,5.);
end_val=put(source_end,5.);
if source=&source_var1 then do;
if start_val ne . then
statement= "IF" ||&source_var1||">"| |"="||(start_val)||" "||'AND'||(end_val)||" THEN "||target_var_name||"'"||strip(target_value)||"'";
else statement= "IF" ||&source_var1||"<"|| (end_val)||" THEN "|| target_var_name||"'"||strip(target_value)||"'";
put ' ' statement ;
end;
run;

%mend grp_code_gen;
options mprint symbolgen;
%grp_code_gen(source_var1='ageY');

the log--->>
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
55
56 %macro grp_code_gen(source_var1=);
57 data _null_;
58 set work.gexcel11;
59 length statement $1000. end_val start_val $5. source $10.;
60 source= &source_var1;
61 start_val=put(source_start,5.);
62 end_val=put(source_end,5.);
63 if source=&source_var1 then do;
64 if start_val ne . then
65 statement= "IF" ||&source_var1||">"| |"="||(start_val)||" "||'AND'||(end_val)||" THEN
65 ! "||target_var_name||"'"||strip(target_value)||"'";
66 else statement= "IF" ||&source_var1||"<"|| (end_val)||" THEN "|| target_var_name||"'"||strip(target_value)||"'";
67 put ' ' statement ;
68 end;
69 run;
70
71 %mend grp_code_gen;
72 options mprint symbolgen;
73 %grp_code_gen(source_var1='ageY');
MPRINT(GRP_CODE_GEN): data _null_;
MPRINT(GRP_CODE_GEN): set work.gexcel11;
MPRINT(GRP_CODE_GEN): length statement $1000. end_val start_val $5. source $10.;
SYMBOLGEN: Macro variable SOURCE_VAR1 resolves to 'ageY'
MPRINT(GRP_CODE_GEN): source= 'ageY';
MPRINT(GRP_CODE_GEN): start_val=put(source_start,5.);
MPRINT(GRP_CODE_GEN): end_val=put(source_end,5.);
SYMBOLGEN: Macro variable SOURCE_VAR1 resolves to 'ageY'
MPRINT(GRP_CODE_GEN): if source='ageY' then do;
SYMBOLGEN: Macro variable SOURCE_VAR1 resolves to 'ageY'
MPRINT(GRP_CODE_GEN): if start_val ne . then statement= "IF" ||'ageY'||">"|"="||(start_val)||" "||'AND'||(end_val)||" THEN
"||target_var_name||"'"||strip(target_value)||"'";
SYMBOLGEN: Macro variable SOURCE_VAR1 resolves to 'ageY'
MPRINT(GRP_CODE_GEN): else statement= "IF" ||'ageY'||"<"|| (end_val)||" THEN "|| target_var_name||"'"||strip(target_value)||"'";
MPRINT(GRP_CODE_GEN): put ' ' statement ;
MPRINT(GRP_CODE_GEN): end;
MPRINT(GRP_CODE_GEN): run;

NOTE: Character values have been converted to numeric values at the places given by: (Line)Smiley SadColumn).
73:219
IFageY>= 65 AND 80 THEN AGEGR1'>=65, < 80'
IFageY< 65 THEN AGEGR1'< 65'
IFageY>= 65 AND . THEN AGEGR1'> =80'
Grand Advisor
Posts: 17,333

Re: debug help

[ Edited ]

So it looks like when you create the variable statement you're missing an = and semicolon at the end of the IF statement. 

 

Add them into the concatenation code.

I personally find it easier to use the CAT function rather than ||, as it's easier to see issues like this.

Solution
‎01-18-2016 01:10 AM
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: debug help

An example, plain base SAS code:

data gexcel11;
  infile datalines dlm="|" missover;
  length command1 command2 target_value $2000;
  input command1 $ command2 $ target_value $;
datalines;
AGEY >= 65|AGEY < 80|>=65, < 80
AGEY < 65| |< 65
AGEY >= 65| |> =80
;
run;

data _null_;
  set gexcel11;
  length command $2000;
  command=cat("if ",catx(' and ',command1,command2)," then agegr1='",strip(target_value),"';");
  put command;
run;
Grand Advisor
Posts: 17,333

Re: debug help

When trying to debug a macro it's helpful to run the code with the following options so the log has more information.

 

options mprint symbolgen;
Regular Contributor
Posts: 190

Re: debug help

@RW9 @Reeza thanks for d inputs
☑ This topic is SOLVED.

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

Discussion stats
  • 11 replies
  • 550 views
  • 0 likes
  • 4 in conversation