DATA Step, Macro, Functions and more

How to understand the following call symput in macro?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 99
Accepted Solution

How to understand the following call symput in macro?

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.


Accepted Solutions
Solution
‎04-01-2012 12:05 AM
Super Contributor
Posts: 1,636

How to understand the following call symput in macro?

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

View solution in original post


All Replies
Frequent Contributor
Posts: 99

How to understand the following call symput in macro?

2 obs of data Temp_cats

IV_BinCountPercent
14108.2
2350.7
Solution
‎04-01-2012 12:05 AM
Super Contributor
Posts: 1,636

How to understand the following call symput in macro?

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

Frequent Contributor
Posts: 99

How to understand the following call symput in macro?

Linlin, thanks for your reply,

can you go further as to tell why left and compress functions are used?

thanks.

Super Contributor
Posts: 1,636

How to understand the following call symput in macro?

"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)Smiley SadColumn).

      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

Frequent Contributor
Posts: 99

How to understand the following call symput in macro?

Linlin,thanks for your answers.

Super User
Super User
Posts: 6,499

Re: How to understand the following call symput in macro?

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";
Frequent Contributor
Posts: 99

How to understand the following call symput in macro?

Tom,

Thanks for your reply.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 1700 views
  • 6 likes
  • 3 in conversation