BookmarkSubscribeRSS Feed
SrikanthY
Calcite | Level 5

I want to set multiple sas data sets with required variables based on condition in a single shot by using sashelp.vcolumns.

 

data all;

set x (keep= a1 a2 b where = (^missing(b))

     y (keep= a1 a2 c where = (^missing(c))

     z (keep= a1 a2 where = (^missing(d))

    zz (keep= a1 a2 e where = (^missing(e))

........ so on....

run;

 

data set o/p
a1a2all_colval
aaa1b1
aaa2c2
bba1d3
bba2e2
6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Sorry, please clarify, do you mean set the dataset if the variable exists, or where the variable contains data?  If its just data, then you could simplify to:

data all;
  set x (keep=a1 a2 all_coll rename=(b=all_coll))
  ...
  where not missing(all_coll);
run; 

Providing some example test data (in the form of a datastep) and what you want to see as output would help.

SrikanthY
Calcite | Level 5
should check if data set exist or not and then variable and then data..
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Still guessing here as your not providing much info.  You can try something like this, assuming your data is in work library, and your list of variables is a and b in every dataset,and c d or f being the ones you want to check:

data _null_;
  set sashelp.vcolumn (where=(libname="WORK" and name in ("C","D","E")) end=last;
  if _n_=1 then call execute('data want; set ');
  call execute(cat(memname,' (keep=a b ',name,' where=(not missing(',name,'))) '));
  if last then call execute(';run;');
run;

So this will for each dataset which has a variable C D or E, add to the set statement which is generated keeping a b and the variable from that list.  

 

SrikanthY
Calcite | Level 5
thank you for your logic. bit I have modified its working.
but I want one more column which variable is getting output.

for example: if variable is 'C' or 'D' or 'E'
then
colmn
C
D
E


modified prog:

data _null_;
set sashelp.vcolumn (where=(libname="WORK" and name in ("C","D","E"))
end=last;
if _n_=1 then call execute('data want; set ');
call execute(cat('libA.',memname,' (keep=a b ',name, 'rename =(',name, '=
col1) where=(not missing(',name,'))) '));
if last then call execute(';run;');
run;


I am getting error when I am trying to include:
call execute('; colmn = ' || name );

ERROR: data step component object failure.


RW9
Diamond | Level 26 RW9
Diamond | Level 26

Have a step after that to do the final part:

data ds1;
  a=1; b=2; c=3;
run;

data ds2;
  a=2; b=3; d=1;
run;

data ds3;
  a=3; b=4; e=5;
run;

data _null_;
  set sashelp.vcolumn (where=(libname="WORK" and upcase(name) in ("C","D","E"))) end=last;
  if _n_=1 then call execute('data want; set ');
  call execute(cat(memname,' (keep=a b ',name,' where=(not missing(',name,'))) '));
  if last then call execute(';run;');
run;

data want (keep=a b col1 from);
  set want;
  array v{*} _numeric_;
  do i=3 to dim(v);
    if v{i} ne . then do;
      col1=v{i};
      from=vname(v{i});
    end;
  end;
run;
Reeza
Super User

How do you know to check if a data set exists? If you're using the SASHELP table, the data set has to exist already so there's something in your logic that's not clear. 

 

Please take the time to provide more details if you want help.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 776 views
  • 0 likes
  • 3 in conversation