turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Keeping Variables based on some condition

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-18-2008 03:36 AM

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to deleted_user

12-18-2008 03:49 AM

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

/Linus

Data never sleeps

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to deleted_user

12-22-2008 05:17 PM

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]

[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.

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]

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]