Help using Base SAS procedures

KEEP more then 2 variables in "IF_N_ = 1 THEN SET ..."

Accepted Solution Solved
Reply
Contributor ZC
Contributor
Posts: 21
Accepted Solution

KEEP more then 2 variables in "IF_N_ = 1 THEN SET ..."


Hello,

When running the following SAS syntax, I found that I can keep maximum two variables (e.g., x1, x2) using "KEEP" after IF .... THEN.... if I keep three or more variables, only two variables have values and other variables are with missing values. Could anyone please tell me how to keep three or more variables?

DATA case_control;

     IF_N_ = 1 THEN SET case (keep = x1 x2 RENAME (x1=height_case x2 = weight_case));

     SET control;

     diff1 = height_case - height;

     diff2 = weight_case - weight;

RUN;


Accepted Solutions
Solution
‎08-03-2013 06:12 PM
Super User
Posts: 5,226

Re: KEEP more then 2 variables in "IF_N_ = 1 THEN SET ..."

SAS doesn't limit the variables you can read in, so the problem lies elsewhere.

I assume that you have already checked the data set CASE to make sure that the values you bring in are not missing to begin with. 

So the problem likely lies in CONTROL.  If that data set also contains the additional variables (and has missing values for them), you would get the results you are observing.

Good luck.

View solution in original post


All Replies
Solution
‎08-03-2013 06:12 PM
Super User
Posts: 5,226

Re: KEEP more then 2 variables in "IF_N_ = 1 THEN SET ..."

SAS doesn't limit the variables you can read in, so the problem lies elsewhere.

I assume that you have already checked the data set CASE to make sure that the values you bring in are not missing to begin with. 

So the problem likely lies in CONTROL.  If that data set also contains the additional variables (and has missing values for them), you would get the results you are observing.

Good luck.

Respected Advisor
Posts: 4,742

Re: KEEP more then 2 variables in "IF_N_ = 1 THEN SET ..."

It could be that the control dataset overwrites some of your variables. Your example, once fixed for the typos (space between IF and _N_ and = after RENAME), works just fine, even with and extra variable in the case dataset.

data case;
x1=1; x2=2; x3=3;
run;

data control;
input height weight;
datalines;
10 100
20 200
30 300
;

DATA case_control;
     IF _N_ = 1 THEN SET case (keep = x1 x2 x3 RENAME=(x1=height_case x2 = weight_case x3=test));
     SET control;
     diff1 = height_case - height;
     diff2 = weight_case - weight;
RUN;

proc print; run;

PG

PG
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 186 views
  • 5 likes
  • 3 in conversation