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;
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";
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()).
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?
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.
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";
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.
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;
@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.
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.
I don't want to print, I would like to get it via Data set.
@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.
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.