BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bbb_NG
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

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

7 REPLIES 7
bbb_NG
Fluorite | Level 6

2 obs of data Temp_cats

IV_BinCountPercent
14108.2
2350.7
Linlin
Lapis Lazuli | Level 10

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

bbb_NG
Fluorite | Level 6

Linlin, thanks for your reply,

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

thanks.

Linlin
Lapis Lazuli | Level 10

"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

bbb_NG
Fluorite | Level 6

Linlin,thanks for your answers.

Tom
Super User Tom
Super User

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";
bbb_NG
Fluorite | Level 6

Tom,

Thanks for your reply.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 7 replies
  • 6955 views
  • 6 likes
  • 3 in conversation