Help using Base SAS procedures

Is this possible in sas?

Reply
Occasional Contributor
Posts: 14

Is this possible in sas?

Hello.

I am supposed to find out if this is possible:

DATA FILE1 FILE2;
SET FILE_IN;

IF X=Y THEN DO;
NEWVAR1=VAR1;
OUTPUT FILE1;
END;

ELSE DO;
NEWVAR2=VAR1;
OUTPUT FILE2;
END;

RUN;


Is it somehow possible that SAS with the help of some keyword only have newvar1 in file1 in addition to all other variables. And in file2 its only consist all variables + newvar2.

This is very simple with drop and keep. But is it possible without, make sas execute partly? May be this is to make it more difficult. But I just want to know if its possible in other way or not. I cannot figure out another way than using drop and keep.

Thanks
Frequent Contributor
Posts: 106

Re: Is this possible in sas?

See the documentation for the RENAME dataset option.
Trusted Advisor
Posts: 2,113

Re: Is this possible in sas?

You could also do it with the KEEP= or DROP= option on the DATA statement.
Contributor
Posts: 57

Re: Is this possible in sas?

Hi,

data file1 file2;
set file_in;
if x=y then output file1;
else output file2;
run;

proc datasets library=work;
modify file1;
rename var1=newvar1;
quit;
run;

proc dataset library=work;
modify file2;
rename var1=newvar2;
quit;
run;

Rename will give unwanted result if it is used within IF condition. Rename is a global statement. It creates the variable irrespective of the conditons checked.
Hence a seperate program for renaming.

Sandhya.
Regular Contributor
Posts: 241

Re: Is this possible in sas?

It is a good and efficient practice to use proc datasets to rename variables, since this will just modify the header portion of the dataset.

Sometimes, however, the data set options can be quite handy as well.

[pre]
/* test data */
data one;
x=1; y=1; var1=1; output;
x=2; y=3; var1=2; output;
x=3; y=2; var1=3; output;
run;

/* separate and rename */
data two(rename=(var1=newvar1))
three(rename=(var1=newvar2));
set one;
if x=y then output two;
else output three;
run;

/* check */
proc print data=two;
run;
proc print data=three;
run;
/* on lst
Obs x y newvar1

1 1 1 1

Obs x y newvar2
1 2 3 2
2 3 2 3
*/
[/pre]
Ask a Question
Discussion stats
  • 4 replies
  • 136 views
  • 0 likes
  • 5 in conversation