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

@LinusH, @ballardw : Sorry, I am bit of slow. I am still not quite understand what you suggestion. I am new to Macro. Could you supply me with some code please?

 

Thanks!

RW9
Diamond | Level 26 RW9
Diamond | Level 26

The concept is called By Group processing, in-built into Base SAS:

http://www2.sas.com/proceedings/forum2007/222-2007.pdf

 

It is pretty simple.  As you have 3 datasets, first combine them, and get this dataset name (dirst step below, the : means all datasets with that prefix), sort them just to make sure and apply the where, then print them by dataset name.

 

data want;
  length dataset $200;
  set eltm.mlt: indsname=tmp;
  dataset=tmp;
run;

proc sort data=want;
  by dataset;
  where cr_appl_no=60048444;
run;
 
proc print data=want;
  by dataset;
run;

Do also note the consistent indentation, casing and such like in the code, it is very important to make code as easy to read as possible.  And you don't need macro for this, or in fact any other task you will have to do.  Macro is an additional text generation tool to save repeating code, it is not a replacement for Base SAS.

Nancy05
Quartz | Level 8

@RW9: I don't have 3 datasets, I have more than 50 datasets. How to combine more than 50 datasets. Could you show me by code please?

 

Thanks!

Reeza
Super User

@Nancy05 Do all your tables have the same columns and same data types?

Nancy05
Quartz | Level 8

@Reeza Yes, My libname is correct  and memname is starting with 'MLT'.

 

I have no problem of running this piece of code, and return the results I expected.

proc sql;
select distinct memname
from sashelp.vtable
where libname ='ELTML' 
and upper(memname) like 'MLT%';
quit;

run;

I think the problem is with Cat function and not sure how to fix it.

 

Reeza
Super User

STR1 is incorrect. It should generate to the following. 

 

tiltle 'My lending table - table name'; 

 

@Nancy05 Do all your lending tables have the same columns? If so, @RW9 is correct and there are much easier methods. 

 

Nancy05
Quartz | Level 8

@Reeza: No. Mylending datasets/table have different varibles/columns.

 

I have comment off STR1, but STR2 also not working... . probably need some space between words, not quite sure how.

 

Even @RW9 's method not sure it is works as I don't want all tables combined together (50 tables - more than 900 fields), it is too long to read, I like your second dynamic sql call and would like to make it working... 

 

Reeza
Super User

If your tables have different columns and variables then @RW9 solution won't work. 

I modified the code above, it's tested and working. 

 

The theory behind it is create a list of the tables using SASHELP.VTABLE to get the names.

Then generate a proc print using the table names and call execute. The strings you need to generate are easy, a title and proc print, with only the table name changing. The table name comes from the table list. 

 

Good Luck. 

Reeza
Super User

STR1 is incorrect. It should generate to the following. 

 

tiltle 'My lending table - table name'; 

 

@Nancy05 Do all your lending tables have the same columns? If so, @RW9 is correct and there are much easier methods. 

 

Reeza
Super User

1. Add SET statement to data _null_ so the table is being used

2. Modify the STR1 to generate the TITLE statement correctly.

 

This is tested and working. 

 

%macro print_report(CR_APPL_NO);

%*Get all table names from library that start with MLT;

proc sql noprint;
create table table_list as
select distinct memname
from sashelp.vtable
where libname='ELTML' 
and upper(memname) like 'MLT%';
quit;

%*use Call execute to print each table for specified record;
%*I create the strings as an extra step to allow for debugging;
%*You should remove the PUT statements after you have this working;
data _null_;

SET TABLE_LIST;

str1=cats( "Title 'Mylending Tables-", upcase(memname), "';");
str2=cat("Proc Print Data=ELTML.", memname, "NOOBS;", "Where CR_APPL_NO=&CR_APPL_NO; RUN;");

put str1;
put str2;

call execute(str1);
call execute(str2);

run;

%mend;
Nancy05
Quartz | Level 8

Thanks everyone who answered my question! Especially, a big thank you to you, @Reeza. Smiley Happy This is a piece of elegent code. This is exactly what I want.  Simple and effcient! Really awesome. Heart

 

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
  • 25 replies
  • 7411 views
  • 3 likes
  • 5 in conversation