DATA Step, Macro, Functions and more

Data step issue.

Accepted Solution Solved
Reply
Super Contributor
Posts: 265
Accepted Solution

Data step issue.

issue with output:

i m trying to giving output as per condition, but its giving all observation to all created dataset.

my desired result is getting by existing code but for that i need to create addition dataset "X". I dont want to create that X dataset.

how can i prevent process to put observation to all.

my code is:

DATA CONTROL;

INPUT BANKCODE $1-6 PRODCODE $9-10 COND $13-36;

DATALINES;

HDFC HL DELAY>10

HDFC CC DELAY<10

ICICI ML DEFAULT=100

ICICI CC DEFAULT<10 AND DELAY<10

IDBI ML WRITE_OFF =10

IDBI CC DELAY<20

;

RUN;

PROC PRINT; RUN;

PROC SQL NOPRINT;

Select bankcode, prodcode, cond into : banklist separated by "|" ,: prodlist separated by "|" ,: condlist separated by "|" from control;

%let ct = &sqlobs;

QUIT;

%put &banklist &prodlist &condlist &ct;

DATA MASTER;

INPUT BANKCODE $1-6 PRODCODE $9-10 DELAY 13-14 DEFAULT 17-18 WRITE_OFF 21-24;

DATALINES;

HDFC CC 5 1 12

ICICI HL 6 4 100

IDBI ML 2 3 20

;

RUN;

PROC PRINT; RUN;

option mlogic symbolgen macrogen;

%macro test();

data hdfc icici idbi x;

set master;

%do i =1 %to &ct;

%let bname=%scan(%bquote(&banklist),&i,%str(|));

%let pname=%scan(%bquote(&prodlist),&i,%str(|));

%let cname=%scan(%bquote(&condlist),&i,%str(|));

%if (bankcode="&bname") and (prodcode="&pname") and (&cname)  %then

%do;

*status= "&cname.";

status=%substr(%bquote(&cname),1,%sysfunc(ANYPUNCT(%bquote(&cname)))-1);

output &bname. ;

%end;

%end;

output x;

run;

%mend;

%test;


Accepted Solutions
Solution
‎09-18-2014 11:28 AM
Super User
Super User
Posts: 7,407

Re: Data step issue.

There are several points here.  Firstly your test dataset CONTROL is not being created as you think it is.  The variable BANKCODE for instance is reading 6 characters in , so HDFC H for first row.  This doesn't match anything in the master table.

Then there is the problem that you are mixing macro code with datastep code.

The %if construct is a macro command, it is there to generate code conditionally - i.e. it isn't part of the datastep.  Thus saying %if a datastep variable called bankcode="&bname."... - this is invalid, as the text "bankcode" != whatever &bname resolves to.  Bank code is part of the datastep and the datastep syntax, % macro commands are part of the macro compiler.  Think of the macro compiler as a text replace function, not part of the SAS code.

Now you are looking to do certain conditions on the dataset correct.  So try the following:

data control;
  length bankcode $6 prodcode $2 cond $200;
  infile datalines dlm=",";
  input bankcode $ prodcode $ cond $;
datalines;
HDFC,HL,DELAY>10
HDFC,CC,DELAY<10
ICICI,ML,DEFAULT=100
ICICI,CC,DEFAULT<10 AND DELAY<10
IDBI,ML,WRITE_OFF =10
IDBI,CC,DELAY<20
;
run;

data master;
  length bankcode $6 prodcode $2;
  infile datalines dlm=",";
  input bankcode $ prodcode $ delay default write_off;
datalines;
HDFC,CC,5,1,12
ICICI,HL,6,4,100
IDBI,ML,2,3,20
;
run;

data _null_;
  set control end=last;
  if _n_=1 then call execute('data hdfc icici idbi; set master;');
  call execute(' if bankcode="'||strip(bankcode)||'" and prodcode="'||strip(prodcode)||'" and '||strip(cond)||' then output '||strip(bankcode)||';');
  if last then call execute(';run;');
run;

View solution in original post


All Replies
Super User
Super User
Posts: 7,407

Re: Data step issue.

I am not following your logic or example.  If you don't want to save X then drop that from here: data hdfc icici idbi x;

And drop this: output x;

Super Contributor
Posts: 265

Re: Data step issue.

Apology for not being clear.

Resultant with corrent code is 0obs is correct.

If i will remove content related with data set x, resultant will be 3 obs to each dataset which is wrong, i dont want that and i also dont want to creat dataset x. I hope i tried to explain my best

Solution
‎09-18-2014 11:28 AM
Super User
Super User
Posts: 7,407

Re: Data step issue.

There are several points here.  Firstly your test dataset CONTROL is not being created as you think it is.  The variable BANKCODE for instance is reading 6 characters in , so HDFC H for first row.  This doesn't match anything in the master table.

Then there is the problem that you are mixing macro code with datastep code.

The %if construct is a macro command, it is there to generate code conditionally - i.e. it isn't part of the datastep.  Thus saying %if a datastep variable called bankcode="&bname."... - this is invalid, as the text "bankcode" != whatever &bname resolves to.  Bank code is part of the datastep and the datastep syntax, % macro commands are part of the macro compiler.  Think of the macro compiler as a text replace function, not part of the SAS code.

Now you are looking to do certain conditions on the dataset correct.  So try the following:

data control;
  length bankcode $6 prodcode $2 cond $200;
  infile datalines dlm=",";
  input bankcode $ prodcode $ cond $;
datalines;
HDFC,HL,DELAY>10
HDFC,CC,DELAY<10
ICICI,ML,DEFAULT=100
ICICI,CC,DEFAULT<10 AND DELAY<10
IDBI,ML,WRITE_OFF =10
IDBI,CC,DELAY<20
;
run;

data master;
  length bankcode $6 prodcode $2;
  infile datalines dlm=",";
  input bankcode $ prodcode $ delay default write_off;
datalines;
HDFC,CC,5,1,12
ICICI,HL,6,4,100
IDBI,ML,2,3,20
;
run;

data _null_;
  set control end=last;
  if _n_=1 then call execute('data hdfc icici idbi; set master;');
  call execute(' if bankcode="'||strip(bankcode)||'" and prodcode="'||strip(prodcode)||'" and '||strip(cond)||' then output '||strip(bankcode)||';');
  if last then call execute(';run;');
run;

Super Contributor
Posts: 265

Re: Data step issue.

Thanks, it makes much easier way.

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 367 views
  • 0 likes
  • 2 in conversation