BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
HannaZhang
Obsidian | Level 7

Hi,,all;

 I  tried to write some macro statements.but they did not work and I can not find the mistakes,here are the codes.Thanks.

%macro T71122;

proc sort data=test2;
	by OBS;
run;
data test3;
	set test2;
	if obs=1 then delete;
run;
data test3;
	set test3;
	obs=obs-1;
run;

proc sql;
	select distinct AMTEST into: AMTESTlist separated by ":" from test3 ;
run;
quit;

%let num=1;
%do %while(%scan(&AMTESTlist.,&num.,%str(:)) NE %str());
	%let AMTESTtest=%scan(&AMTESTlist.,&num.,%str(:));
%let num=%eval(&num.+1);

%end;

%mend;
%T71122;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

The code seems fine,but there is issue with the data i.e. in AMTEST there is text as '榓-'

the special characters concatenated to HCV and these special characters need to be removed using compress function and it will resolve the issue.

 

%macro T71122;

data test3;
	set test.test2 (ENCODING=WLATIN1);
	if obs=1 then delete;
run;

data test3;
	set test3;
	obs=obs-1;
run;

proc sql;
	select distinct compress(AMTEST,'?榓-') into: AMTESTlist separated by ":" from test3 ;
quit;

%put &AMTESTlist.;

%let num=1;
%do %while(%scan(&AMTESTlist.,&num.,%str(:)) NE %str());
	%let AMTESTtest=%scan(&AMTESTlist.,&num.,%str(:));
%let num=%eval(&num.+1);
%put #
%end;

%mend;
options mprint;
%T71122;
Thanks,
Jag

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Macro variables have scope. If created in a macro, they are local to that macro and vanish as soon as the macro stops executing.

 

You can simplify the macro loop by using the automatic macro variable sqlobs:

%do num = 1 %to &sqlobs.;
  %let AMTESTtest=%scan(&AMTESTlist.,&num.,:);
%end;

If you have further problems, post the log by using the {i} button.

Jagadishkatam
Amethyst | Level 16

The code seems fine,but there is issue with the data i.e. in AMTEST there is text as '榓-'

the special characters concatenated to HCV and these special characters need to be removed using compress function and it will resolve the issue.

 

%macro T71122;

data test3;
	set test.test2 (ENCODING=WLATIN1);
	if obs=1 then delete;
run;

data test3;
	set test3;
	obs=obs-1;
run;

proc sql;
	select distinct compress(AMTEST,'?榓-') into: AMTESTlist separated by ":" from test3 ;
quit;

%put &AMTESTlist.;

%let num=1;
%do %while(%scan(&AMTESTlist.,&num.,%str(:)) NE %str());
	%let AMTESTtest=%scan(&AMTESTlist.,&num.,%str(:));
%let num=%eval(&num.+1);
%put #
%end;

%mend;
options mprint;
%T71122;
Thanks,
Jag
Astounding
PROC Star
The only place that a hidden %EVAL might be invoked is within the comparison inside the %DO %WHILE loop. For that comparison, try switching from %SCAN to %QSCAN.
HannaZhang
Obsidian | Level 7
Thanks,I konw where the problem is.Your suggestion is very helpful.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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