DATA Step, Macro, Functions and more

SAS query

Reply
Contributor scb
Contributor
Posts: 69

SAS query

[ Edited ]

When I run the below code, I got the report generated perfectly but got one error message - "ERROR: The value 000485 is not a valid SAS name."

Can anyone help me on this? Thanks.

 


DATA FINAL2;
INPUT SALES_STAFF_ID $ PD $ PAYOUT;
DATALINES;
000485 ABC 1000
;
run;

 

data _null_;
set FINAL2 end=eof;
by SALES_STAFF_ID;
flag+1;
call symput('var'||put(flag,8. -L),SALES_STAFF_ID);
end;
if eof then call symput('tot',put(flag,8. -L));
run;

%macro groups(dsn,byvar);
%do i=1 %to &tot;
data "&&var&i";
set &dsn;
if &byvar="&&var&i" then output;
run;

proc sql noprint;
select distinct Sales_STAFF_ID
into Smiley SurprisedRDERedvars2 separated by ""
from FINAL2
where SALES_STAFF_ID eq "&&var&i";
quit;

options nodate nonumber center;
ods escapechar="^";
title;
footnote;

%LET OUTPATH = D:\TESTING-&orderedvars2..PDF;

ODS PDF FILE="&OUTPATH" compress=9 startpage=NO;

options nocenter;
options orientation=landscape papersize=A3;
*options orientation=portrait papersize=A3;
ods pdf startpage=now;
ods pdf style=analysis;

ODS TEXT = "OVERALL RESULT";
PROC PRINT DATA=FINAL2(WHERE=(SALES_STAFF_ID = "&&var&i")) NOOBS LABEL;
LABEL SALES_STAFF_ID='ID'
PD="POST"
PAYOUT="Payout";
FORMAT PAYOUT DOLLAR18.2;
RUN;

ODS PDF CLOSE;
ODS LISTING;
ODS LISTING CLOSE;

%end;
%mend groups;

options mprint symbolgen;
%groups(FINAL2,SALES_STAFF_ID)

Trusted Advisor
Posts: 1,932

Re: SAS query

You are creating a macro variable with the value 000485 (from SALES_STAFF_ID) and then you are using that macro variable with value 000485 to be a data set name, which cannot be a data set name, data set names must begin with a character or an underscore. Also, you wouldn't put the macro variables in quotes in the DATA statement.

PROC Star
Posts: 768

Re: SAS query

A dataset name cannot begin with a number

Super User
Super User
Posts: 7,997

Re: SAS query

Why are you doing all that in the first place?  It looks like your creating a file/report for each subject in the first datastep, so why not (also please avoid using all upper case or mixing, it makes it hard to read):

data final2;
  input sales_staff_id $ pd $ payout;
datalines;
000485 ABC 1000
;
run;

data _null_;
  set final2;
  call execute('ods pdf file="d:\testing-'||strip(sales_staff)||'.pdf" compress=9 startpage=no;
                options nocenter;
                options orientation=landscape papersize=a3;
                ods pdf startpage=now;
                ods pdf style=analysis;
                ods text = "overall result";
                proc print data=final2(where=(sales_staff_id ='||strip(sales_staff)||')) noobs label;
                  label sales_staff_id="id"
                  pd="Post"
                  payout="Payout";
                  format payout dollar18.2;
                run;
                ods pdf close;');
run;
Trusted Advisor
Posts: 1,932

Re: SAS query


RW9 wrote:

Why are you doing all that in the first place?  It looks like your creating a file/report for each subject in the first datastep, so why not (also please avoid using all upper case or mixing, it makes it hard to read):


Perhaps the user thinks, rightly so, that this use of macros is a supported tool in SAS, that should produce the answer (s)he wants, if he can fix his code; and along the way learn something about how macros work. Also, the SAS documentation has lots of examples of writing macro code to perform tasks similar to this, why shouldn't the user do this?

 

 

Super User
Super User
Posts: 7,997

Re: SAS query

Posted in reply to PaigeMiller

Indeed, and generates a lot more code to do looping which is already taken care of by the data step.  Another version would be to write the macro, then use the datastep to generate the calls to the macro, again avoiding the need to dop looping and macro lists.  It is hardly my fault if this is not in the documentation as I have no control over that, and people will not know this is it is not shown.  You can also learn a lot more seeing various methods of doing the same thing.

Trusted Advisor
Posts: 1,932

Re: SAS query


RW9 wrote:

Indeed, and generates a lot more code to do looping which is already taken care of by the data step.


Why is this a concern? Shouldn't the initial concern be to get the code to do what you want?

 

You can also learn a lot more seeing various methods of doing the same thing.

 

You could approach the situation in a little less confrontational way. Instead of "Why are you doing all that in the first place?" you could indicate that a better approach is ... and then explain why it is better ... your tone doesn't really lend itself to have people consider your suggestion.

Ask a Question
Discussion stats
  • 6 replies
  • 174 views
  • 0 likes
  • 4 in conversation