Keep only the variables in a data set that end in a particular character

by Irina 23. May 2007 12:03
 

data

one;

input abab ret cca www ttt ggab;

datalines

;

1 2 3 4 5 6

;

options

mprint;

%macro vars(dsn,chr);

%local list dsid num len tmp rc;

%let list=;

%let dsid=%sysfunc(open(&dsn));

%let num=%sysfunc(attrn(&dsid,nvars));

%let len=%length(&chr);

%do i = 1%to #

%let var&i=%sysfunc(varname(&dsid,&i));

%let tmp=%sysfunc(reverse(&&var&i));

%if%sysfunc(reverse(%upcase(%substr(&tmp,1,&len)))) = %upcase(&chr) %then%do;

%let list=&list &&var&i;

%end;

%end;

%let rc=%sysfunc(close(&dsid));

%if

&list ne %then%do;

data final;

set &dsn(keep=&list);

run;

proc print data=final;

run;

%end

;

%else

%do;

data _null_;

file print;

put

"No variables found in dataset &dsn that end with &chr";

run;

%end

;

%mend

vars;

/* Invoke the macro passing in the name of the data set */

/* that contains all your variables. Also pass it the */

/* characters you would like the variables to end with. */

%vars(one,ab)

%sysfunc

by Irina 22. May 2007 06:40

%sysfunc :

%sysfunc is useful for retrieving information about datasets, changing column attributes and formatting macro variables.One primary difference between calling a function in %SYSFUNC and elsewhere in the SAS System is this last notion of applying a format to the results of a function call.

Example1.

footnote finished: %sysfunc (today(), date9. );

Example2.

%put %sysfunc (PutN(&Salary,dollar12.));

Example3.

%put With a salary of
%sysfunc (PutN(&Salary,dollar12.));
%put You are %sysfunc(PutN(&salary, income.));

Example4.

If you wanted to find the total number of observations in a dataset and store that number as a macro variable. You might use something like this:

%let dsid= %sysfunc (open(name of the data));
%let nvars= %sysfunc (attrn(&dsid,nlobs));
%put &nvars; 
 

Tags: %sysfunc, macro

SAS | macro

PROC SQL: An Efficient Tool for Creating Macro Variables

by Irina 30. April 2007 12:15
PROC SQL offers a lot of useful features, which includes, but is not limited to:

 1) Combine the functionality of DATA and PROC steps into one single step.

 2) sort, summarize, join (merge) and concatenate datasets.

 3) Construct in-line views using the FROM and SELECT clauses.

 4) Line up multiple macro variables using the INTO clause.

EXAMPLE 1 .

The SAS code displayed here can create five sets of macro variables, which are used to create  profiles for 16 states.

proc sql;
select compress(put(count(distinct STATE),best12.))
into :counter
from sashelp.Prdsal2 ;
quit;
%put &counter;


proc sql ;
select state,
min_value,
max_value,
min_act,
max_act
into :state1-:state&counter, :min_value1-:min_value&counter,
:max_value1-:max_value&counter, :min_act1-:min_act&counter,
:max_act1-:max_act&counter
from (select STATE,
min(predict) as min_value,
max(predict) as max_value,
min(actual)-3 as min_act,
max(actual)+3 as max_act
from sashelp.Prdsal2
group by STATE);
quit;

EXAMPLE 2.

This is an example on efficiency of PROC SQL in creating macro variables other than the advantage of shortening the code. These two macro variables are used for two different reasons. Macro date_newd is used to specify the tick marks of the horizontal axis by day. However, macro date_newis used to label the tick marks on the horizontal axis.

proc sql ;
select quote(put(date_new,date7.)),
quote(put(date_new,date7.))||'d'
into  :date_new separated by ' ',
       :date_newd separated by ' '
from
(select distinct date as date_new
 from sashelp.buy);
quit;

Tags: sql, macro

SAS | macro

Automatic building of logistic model.

by Irina 21. April 2007 09:19

Description: The data is a collection of information on colleges and universities ( only for example and not pretend to be real). The primary interest is in predicting of graduation . Potential predictor variables are tuition, income, wealth and grades on different subjectes-  200 rows .

The process:

1.

The first section of code splits the file into modeling and validation data sets. Validation sets constructed from the 50/50 stratified sample should be adequate for the purposes of this exercise. I took 95/5 only for example and because of very small data

				
						
DATA model_college;   
                                          
 SCAN: SET  college  end=eof; 
        N+1; 
        IF NOT eof THEN GOTO SCAN; 
        K=0.95*N;       * K IS THE NUMBER TO RANDOMLY SELECT  
                               IT MAY BE A FUNCTION OF N, 
                               E.G.: K=.05*N FOR A 5 PERCENT SAMPLE; 
 LOOP: SET  college ; 
       PROB=K/N;        * PROB IS THE CURRENT SELECT PROBABILITY;
IF RANUNI(123467)>PROB THEN GOTO NEXT; OUTPUT; K=K-1; * THE OBSERVATION IS SELECTED; NEXT: N=N-1; IF N>0 THEN GOTO LOOP; RUN;
For the next steps go to: Automatic building of logistic model

Data for model:   college.txt (10.17 kb)

Tags: logistic

SAS | macro

About the author

Irina Spivak Irina Spivak
Team Leader at G-Stat. More...


Send mail Email

Authors

Blogroll

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2010

    Sign in

    eXTReMe Tracker