DATA Step, Macro, Functions and more

Keeping Variables based on some condition

Reply
N/A
Posts: 0

Keeping Variables based on some condition

Here is my problem

I have two data sets (A and B)

A contains 10 variables and 50 observations.
B contains information about some (not all) variables of A.
Say in this case B has 8 variables, (all of them are in A too) and only 1 observation, which say 'Y' if that particular variable is to be used into analysis otherwise 'N'.

I need to make a final Data set, which has variable which can be used in analysis which all observations from dataset A.

Any Guidance?
Thanks
Super User
Posts: 5,257

Re: Keeping Variables based on some condition

You can read B, and based on the information there, create a macro variable (CALL SYMPUT) that contains a KEEP list which can be used when reading A in the next step.

/Linus
Data never sleeps
SAS Employee
Posts: 160

Re: Keeping Variables based on some condition

I imagine that data A looks like

[pre]data A;
input a b c d e f g h i;
datalines;
1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1
3 4 5 6 1 2 7 8 9
;
run;[/pre]

From here I have to suggetions based on two different B's - both will result in the same 'new' A called analysis.

1)[pre]data B;
length var used $1;
input var used;
datalines;
B Y
C Y
F Y
A N
;
run;

data _null_;
length _x_ $2000;
retain _x_;
set B(where=(used='Y')) end=last;
_x_ = catx(' ',_x_,var);
if last then call symput('a_vars',strip(_x_));
run;
%put &a_vars.;

data analysis;
set A(keep=&a_vars.);
run;[/pre]

2)[pre]data B;
length B C F $1;
input b c f a;
datalines;
Y Y Y N
;
run;

proc sql NOPRINT;
select name into :varnames SEPARATED by ' '
from sashelp.vcolumn
where libname = 'WORK' and memname = 'B';

select nvar into :nvar
from sashelp.vtable
where libname = 'WORK' and memname = 'INPUT';
quit;

%put &varnames.;

data _null_;
length x $44 _x_ $2000;
set B;
do i = 1 to &nvar;
x=scan("&varnames",i,' ');
v = vvaluex(x); put v=;
if v = 'Y' then _x_ = catx(' ',_x_,x);
end;
call symput('a_vars',_x_);
run;

%put &a_vars;

data analysis;
set A(keep=&a_vars.);
run;[/pre]
Ask a Question
Discussion stats
  • 2 replies
  • 90 views
  • 0 likes
  • 3 in conversation