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

Hi,

Need help on running the macro to declare numbered macro variables like pcode1, pcode2....


data  MESTGT.include_processes;

length pname $20.;

cards;

Finalsim

;

run;
data  _null_;

set

MESTGT.include_processes;

symput ('pcode'||_n_,pname);

symput ('nobs', _n_);

;

run;

Error is below -


NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).

      30:23   31:22  

ERROR: Symbolic variable name PCODE           1 must contain only letters, digits, and underscores.

NOTE: Invalid argument to function SYMPUT at line 30 column 6.

pname=  _ERROR_=1 _N_=1

NOTE: The SAS System stopped processing this step because of errors.

NOTE: There were 1 observations read from the data set MESTGT.INCLUDE_PROCESSES.

NOTE: DATA statement used (Total process time):

      real time           0.01 seconds

      cpu time            0.01 seconds


Thanks,
saspert.





1 ACCEPTED SOLUTION

Accepted Solutions
CTorres
Quartz | Level 8

You have several errors:

1. The input statement is missing in the first data step.

2. The correct use od sysmput is call symput.

3. You need to eliminate the spaces in _N_. Maybe using the Strip function.

The correct syntax would be:

Data  include_processes;

length pname $20.;

input pname;

cards;

Finalsim

;

run;

data  _null_;

set include_processes;

call symput ('pcode'||strip(_n_),pname);

call symput ('nobs',strip(_n_));

;

run;

Regards,

View solution in original post

3 REPLIES 3
CTorres
Quartz | Level 8

You have several errors:

1. The input statement is missing in the first data step.

2. The correct use od sysmput is call symput.

3. You need to eliminate the spaces in _N_. Maybe using the Strip function.

The correct syntax would be:

Data  include_processes;

length pname $20.;

input pname;

cards;

Finalsim

;

run;

data  _null_;

set include_processes;

call symput ('pcode'||strip(_n_),pname);

call symput ('nobs',strip(_n_));

;

run;

Regards,

saspert
Pyrite | Level 9

Thanks CTorres,

I just noticed I was missing the input statement. I had the call symput function in my code but it got stripped out while copy- pasting here. I did not know you could use the strip function which is very helpful.

Vince28_Statcan
Quartz | Level 8

Hi Saspert,

Quick good practice or trick to avoid having to debug removing leading or trailing blanks from SAS datasets character variables. The macro-facility with proc sql has exactly what you wanted to do "automated".

You could've done the following:

proc sql noprint;

     select pname

     into : pcode1-: pcode99999999
     from MESTGT.include_processes;
quit;

%let nobs=&sqlobs;

*You will have to remove the space between the column and the macro variable names, I couldn't figure out how to disable the annoying emoticons.

SQLOBS is an automatic variable set to the number of records of the last succesful sql query. Only &SQLOBS pcode variables are created, 99999999 was just an arbitrarily high number to make sure there are enough predefined.

The drawback is that if you use this within a macro, the &sqlobs gets replaced at compilation instead of during executing leading to a fault. You should be able to go around that with %nrstr or simply by doing

proc sql noprint;   

     select pname, count(pname)

          into : pcode1-: pcode99999999, :nobs
          from MESTGT.include_processes
          ;
quit;

Hope this was useful.

Vincent

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 5885 views
  • 0 likes
  • 3 in conversation