DATA Step, Macro, Functions and more

SAS Macro error

Accepted Solution Solved
Reply
Super Contributor
Posts: 268
Accepted Solution

SAS Macro error

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

      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.






Accepted Solutions
Solution
‎07-11-2013 12:22 PM
Regular Contributor
Posts: 180

Re: SAS Macro error

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


All Replies
Solution
‎07-11-2013 12:22 PM
Regular Contributor
Posts: 180

Re: SAS Macro error

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,

Super Contributor
Posts: 268

Re: SAS Macro error

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.

Super Contributor
Posts: 339

Re: SAS Macro error

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

☑ This topic is SOLVED.

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

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