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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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