BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
2 REPLIES 2
LinusH
Tourmaline | Level 20
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
GertNissen
Barite | Level 11
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]

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1251 views
  • 0 likes
  • 3 in conversation