BookmarkSubscribeRSS Feed
ybz12003
Rhodochrosite | Level 12

Hi experts:

 

I have Call Symput program shown below.   My macro Count '_n_' is 781.   I would like to get the 'sname' and 'lname' labeled as below  from 1st to the last (781).  Please advice how to approach this, maybe do loop?  Thanks.

 

data want;
			set test end=last;
			call symput('sname'||left(put(_n_,3.)),left(trim(Length)));
			call symput('lname'||left(put(_n_,3.)),left(trim(label)));
			if last then call symput('count',_n_);
run;

%put &sname1 &lname1 &sname781 &lname781 &count;

%macro ttt;
	 %do i=1 %to &count;
	 %put &i;
		label &&sname&i="&&lname&i";
	 %end;
%mend ttt;
%ttt;

 

 

The Log is shown error message. 

MLOGIC(TTT): %DO loop index variable I is now 781; loop will iterate again.

MLOGIC(TTT): %PUT &i

SYMBOLGEN: Macro variable I resolves to 781

781

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable I resolves to 781

SYMBOLGEN: Macro variable SNAME781 resolves to Culture4

NOTE: Line generated by the invoked macro "TTT".

1561 label &&sname&i="&&lname&i";

-----

180

ERROR 180-322: Statement is not valid or it is used out of proper order.

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable I resolves to 781

SYMBOLGEN: Macro variable LNAME781 resolves to CHART REVIEW 2 Culture4: Other

culture

MPRINT(TTT): label Culture4="CHART REVIEW 2 Culture4: Other culture

result";

11 REPLIES 11
RW9
Diamond | Level 26 RW9
Diamond | Level 26

And what is not working with that?  I have tried it with data I have - noting that you have not provided any test data in the form of datastep requested many times before - and it works fine:

data want;
   set sashelp.class end=last;
   call symput(cats('sname',put(_n_,3.)),strip(name));
   if last then call symput('count',_n_);
run;
%put &sname1 &sname2 &count;

Do note, you can use cat functions to simplfy code, and strip() rather than trim(left()).

ybz12003
Rhodochrosite | Level 12

So, I created some codes followed the Call Symput codes.

 

%macro ttt;
	 %do i=1 %to &count;
	 %put &i;
		label &&sname&i="&&lname&i";
	 %end;
%mend ttt;

%ttt;

 

But I got an error message shown below.

 

1561 label &&sname&i="&&lname&i";

-----

180

ERROR 180-322: Statement is not valid or it is used out of proper order.

 

What is wrong with my codes?

PaigeMiller
Diamond | Level 26

This macro can be used only where a LABEL statement is allowed, in a data step or in certain PROCs. And even there, you have to use it in a way that results in valid SAS code being generated. Show us how you are using it. Show us the surrounding blocks of code and a larger part of the SASLOG.

--
Paige Miller
ybz12003
Rhodochrosite | Level 12

The full error messages are list below.

 

MLOGIC(TTT): %DO loop index variable I is now 780; loop will iterate again.

MLOGIC(TTT): %PUT &i

SYMBOLGEN: Macro variable I resolves to 780

780

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable I resolves to 780

SYMBOLGEN: Macro variable SNAME780 resolves to CultureType4

NOTE: Line generated by the invoked macro "TTT".

1559 label &&sname&i="&&lname&i";

-----

180

ERROR 180-322: Statement is not valid or it is used out of proper order.

SYMBOLGEN: && resolves to &.

SYMBOLGEN: Macro variable I resolves to 780

SYMBOLGEN: Macro variable LNAME780 resolves to CHART REVIEW 2 CultureType4: If

other, specify

MPRINT(TTT): label CultureType4="CHART REVIEW 2 CultureType4: If other, specify";

PaigeMiller
Diamond | Level 26

I said ... show us the surrounding blocks of code and a larger part of the SASLOG. Let's see the code. Let's see more of the SASLOG. 

 

 

--
Paige Miller
Tom
Super User Tom
Super User

Looks like the macro is working fine.

MPRINT(TTT): label CultureType4="CHART REVIEW 2 CultureType4: If other, specify";

You just need to call it in a place where LABEL is a valid statement.

data want ;
  %ttt;
run;
PaigeMiller
Diamond | Level 26

@Tom wrote:

Looks like the macro is working fine.

MPRINT(TTT): label CultureType4="CHART REVIEW 2 CultureType4: If other, specify";

You just need to call it in a place where LABEL is a valid statement.

data want ;
  %ttt;
run;

Yes, this has already been pointed out to @ybz12003, and you can also find this in his partial log file

 

1561 label &&sname&i="&&lname&i";

-----

180

so until we can see his code, AND a larger part of his SASLOG, we won't be able to figure out what is going on.

--
Paige Miller
PaigeMiller
Diamond | Level 26

If you really want to print ALL of these macro variables, this is how to do it.

 

%macro print;
%do i=1 %to &count;
    %put &=i &&sname&i &&lname&i;
%end;
%mend;
%print

But why go through that effort? You can print these values from the SAS data set WANT via PROC PRINT.

--
Paige Miller
ybz12003
Rhodochrosite | Level 12

I don't want to print, I would like to get it via Data set.

PaigeMiller
Diamond | Level 26

@ybz12003 wrote:

I don't want to print, I would like to get it via Data set.


But its already in a data set. Why are you creating macro variables if you want something in a data set? I don't understand.

--
Paige Miller
data_null__
Jade | Level 19

You could CALL EXECUTE print them as you go.  It's a bit fiddly.

 

34   data _null_;
35      set sashelp.class end=last;
36      sname=name;
37      lname=trim(reverse(name));
38      length n $8;
39      n=put(_n_,8.-l);
40      call symputX('sname'||n,sname,'G');
41      call symputX('lname'||n,lname,'G');
42      call execute(cats('%put NOTE: &=sname',n,'- &=lname',n,';'));
43      if last then call symputX('count',_n_);
44      run;

NOTE: SNAME1=Alfred- LNAME1=derflA
NOTE: SNAME2=Alice- LNAME2=ecilA
NOTE: SNAME3=Barbara- LNAME3=arabraB
NOTE: SNAME4=Carol- LNAME4=loraC
NOTE: SNAME5=Henry- LNAME5=yrneH
NOTE: SNAME6=James- LNAME6=semaJ
NOTE: SNAME7=Jane- LNAME7=enaJ
NOTE: SNAME8=Janet- LNAME8=tenaJ
NOTE: SNAME9=Jeffrey- LNAME9=yerffeJ
NOTE: SNAME10=John- LNAME10=nhoJ
NOTE: SNAME11=Joyce- LNAME11=ecyoJ
NOTE: SNAME12=Judy- LNAME12=yduJ
NOTE: SNAME13=Louise- LNAME13=esiuoL
NOTE: SNAME14=Mary- LNAME14=yraM
NOTE: SNAME15=Philip- LNAME15=pilihP
NOTE: SNAME16=Robert- LNAME16=treboR
NOTE: SNAME17=Ronald- LNAME17=dlanoR
NOTE: SNAME18=Thomas- LNAME18=samohT
NOTE: SNAME19=William- LNAME19=mailliW
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

NOTE: There were 19 observations read from the data set SASHELP.CLASS.

NOTE: CALL EXECUTE routine executed successfully, but no SAS statements were generated.

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
  • 11 replies
  • 1355 views
  • 0 likes
  • 5 in conversation