Dear all,
I was learning macro from books, there some code as below in a macro,
Data _null_;
set Temp_Cats;
call symput ("C_" || left(_N_), compress(IV_Bin));
call symput ("n_" || left(_N_), left(count));
call symput ("M", left(_N_));
Run;
I can not have a visual sightseeing as how the 3 call symput did, no matter in log /output window.
Can anyone help to explain what the 3 call symput do?
very thanks.
to create macro variables &c_1 &c_2 &n_1 &n_2 &m;
"C_" || left(_N_) is macro name, when _n_=1 , "C_" || left(_N_) becomes c_1. compress(IV_Bin) is macro value, when _n_=1 , compress(IV_Bin) is 1. so &c_1=1;
run the code below and check the log file.
data temp_cats;
input IV_Bin count;
cards;
1 410
2 35
;
run;
Data _null_;
set Temp_Cats;
call symput ("C_" || left(_N_), compress(IV_Bin));
call symput ("n_" || left(_N_), left(count));
call symput ("M", left(_N_));
Run;
%put &c_1 &c_2 &n_1 &n_2 &m;
Linlin
2 obs of data Temp_cats
IV_Bin | Count | Percent |
1 | 410 | 8.2 |
2 | 35 | 0.7 |
to create macro variables &c_1 &c_2 &n_1 &n_2 &m;
"C_" || left(_N_) is macro name, when _n_=1 , "C_" || left(_N_) becomes c_1. compress(IV_Bin) is macro value, when _n_=1 , compress(IV_Bin) is 1. so &c_1=1;
run the code below and check the log file.
data temp_cats;
input IV_Bin count;
cards;
1 410
2 35
;
run;
Data _null_;
set Temp_Cats;
call symput ("C_" || left(_N_), compress(IV_Bin));
call symput ("n_" || left(_N_), left(count));
call symput ("M", left(_N_));
Run;
%put &c_1 &c_2 &n_1 &n_2 &m;
Linlin
Linlin, thanks for your reply,
can you go further as to tell why left and compress functions are used?
thanks.
"left" moves the number to left. without using "left" function, "c_1" will be "c_ 1". "compress" also takes out extra spaces.
Below is the log file without using left function:
1260 Data _null_;
1261 set Temp_Cats;
1262 call symput ("C_" || (_N_), compress(IV_Bin));
1263 call symput ("n_" || (_N_), left(count));
1264 call symput ("M", _N_);
1265 Run;
NOTE: Numeric values have been converted to character
values at the places given by: (Line):(Column).
1262:31 1262:46 1263:31 1263:42 1264:27
ERROR: Symbolic variable name C_ 1 must contain only letters, digits,
and underscores.
NOTE: Invalid argument to function SYMPUT at line 1262 column 14.
ERROR: Symbolic variable name N_ 1 must contain only letters, digits,
and underscores.
NOTE: Invalid argument to function SYMPUT at line 1263 column 14.
IV_Bin=1 count=410 _ERROR_=1 _N_=1
ERROR: Symbolic variable name C_ 2 must contain only letters, digits,
and underscores.
NOTE: Invalid argument to function SYMPUT at line 1262 column 14.
ERROR: Symbolic variable name N_ 2 must contain only letters, digits,
and underscores.
NOTE: Invalid argument to function SYMPUT at line 1263 column 14.
IV_Bin=2 count=35 _ERROR_=1 _N_=2
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 2 observations read from the data set WORK.TEMP_CATS.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
Linlin,thanks for your answers.
If there are two observations then it will generate 5 macro variables. Equivalent to :
%let C_1=1
%let N_1=410;
%let M=1;
%let C_2=2;
%let N_2=35;
%let M=2;
You can check this by using %PUT statements after the data step.
%put c_1="&c_1" n_1="&n_1" c_2="&c_2" n_2="&N_2" m="&m";
Tom,
Thanks for your reply.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.