BookmarkSubscribeRSS Feed
khandelwalanmol
Fluorite | Level 6

I am trying to build a dataset containing all the column headers which are not null for a particular condition against a column.

example: here is my sample dataset. I have to create a lookup table containing the columns which are not null for individual nmonref column

Sample dataset     
nmonreffi_transaction_idaccount_reference_xidclient_xidcomment_strgcontact_method_xcd
20008809712344567NULLCOMMENT1PHONE
1004880971235NULLABCNULLEMAIL
20008809712364568NULLCOMMENT2PHONE
1008880971237NULLMNOCOMMENT4NULL
1004880971238NULLDEFNULLEMAIL
20008809712394569NULLCOMMENT3PHONE
1008880971240NULLPQRCOMMENT5NULL
1008880971241NULLTUVCOMMENT6NULL

 

Output dataset: 

OUTPUT 
1004fi_transaction_id
1004client_xid
1004contact_method_xcd
  
1008fi_transaction_id
1008client_xid
1008comment_strg
2000fi_transaction_id
2000account_reference_xid
2000comment_strg
2000contact_method_xcd

This  sample data is in a warehouse, so i am restricted to use pass through SQL to create this lookup.

7 REPLIES 7
Tom
Super User Tom
Super User

Sounds like this simple enough, if cumbersome, code is all you need.

select distinct nmonref, 'fi_transaction_id' as varname from have where fi_transaction_id is not null
union 
select distinct nmonref, 'client_xid' as varname from have where client_xid is not null
....

If the variable names change then if you have the list of variable names it is a simple code generation problem to generate that code from data.

 

You should also check if the database you are connected to has any SQL enhancements that might make it easier.  Something similar to PROC TRANSPOSE.

khandelwalanmol
Fluorite | Level 6
the table contains about 120+ columns to be checked, so i am looking out for something dynamic by which i can create that lookup. And the total rows are about 10k+.
Tom
Super User Tom
Super User

You might consider if SAS can do it for you.  PROC FREQ has a nice feature called NLEVELS in PROC FREQ.  You could use nmonref as a by variable.

 

See if you can adapt this code to your situation:

ods select none;
ods output nlevels=nlevels;
proc freq data=sashelp.cars nlevels;
  by make;
  tables _all_ / noprint;
run;
ods select all;
proc print data=nlevels;
  where NNonMissLevels>0;
run;
Mike_j
SAS Employee

The first method that comes to mind is creating a table nmonref and one of the 5 other variables (5 tables in total) and then append all the tables (group and sort as needed)

Tom
Super User Tom
Super User

I am having a hard time understanding how the body of your question has anything to do with the title.

 

Filter out columns if they are not null for a where condition

What does "filter out columns" mean?

What is not null mean in this context?  Assuming by COLUMN you mean VARIABLE does this mean the variable is never missing?  Or just not all missing?

And how does this have anything to do with a WHERE condition?  A WHERE condition to do what exactly?

khandelwalanmol
Fluorite | Level 6
My Bad i drafted the title in a confusing way. I am looking out for a solution where i can build up a lookup table based on nmonref column which will carry which all columns which are populated against a nmonref. for eg, select all non null columns from table where nmonref='2000'.
Patrick
Opal | Level 21

Besides of what @Tom proposes especially with using Proc Freq you can of course also use explicit pass-through SQL as this will allow you to take full advantage of database functionality. 

 

How such code would need to look like depends on your database. Here an example for Oracle.
https://community.oracle.com/tech/developers/discussion/619474/how-to-get-a-list-of-all-columns-that... 

 

If you're not using an "exotic" database then I'd expect some Internet search should return DB specific code that's already close to what you need.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 2403 views
  • 1 like
  • 4 in conversation