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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Aman4SAS
Obsidian | Level 7

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Aman4SAS
Obsidian | Level 7

Thanks, it makes much easier way.

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
  • 4 replies
  • 1018 views
  • 0 likes
  • 2 in conversation