Categorizing variabels in SAS using a range system

人走茶凉 提交于 2019-11-27 08:51:56

问题


I have the numeric values of salaries of different employee's. I want to break the ranges up into categories. However I do not want a new column rather, I want to just format the existing salary column into this range method:

At least $20,000 but less than $100,000 -

At least $100,000 and up to $500,000 - >$100,000

Missing - Missing salary

Any other value - Invalid salary

I've done something similar with gender. I just want to use the proc print and format command to show salary and gender.

DATA Work.nonsales2;
SET Work.nonsales;
RUN;

PROC FORMAT; 
VALUE $Gender 
'M'='Male' 
'F'='Female' 
'O'='Other'  
other='Invalid Code';

PROC FORMAT; 
VALUE salrange 
'At least $20,000 but less than $100,000    '=<$100,000 
 other='Invalid Code';


PROC PRINT;
title 'Salary and Gender';
title2 'for Non-Sales Employees';
format gender $gender.;
RUN;

回答1:


Proc Format is the correct method and you need a numeric format:

 proc format;
 value salfmt
 20000 - <100000 = "At least $20,000 but less than $100,000"
 100000 - 500000 = "100,000 +"
 . = 'Missing'
 other = 'Other';

Then in your print apply the format, similar to what you did for gender.

format salary salfmt.;

This should help get you started.




回答2:


I created a little function that mimics the R cut functions :

options cmplib=work.functions;
proc fcmp outlib=work.functions.test;
function cut2string(var, cutoffs[*], values[*] $) $;
    if var <cutoffs[1] then return (values[1]);
    if var >=cutoffs[dim(cutoffs)] then return (values[dim(values)]);
    do i=1 to dim(cutoffs);
        if var >=cutoffs[i] & var <cutoffs[i+1] then return (values[i+1]); 
    end;
    return ("Error, this shouldn't ever happen");

endsub;

run;

Then you can use it like this :

data Work.nonsales2;
    set Work.nonsales;
    array cutoffs[3] _temporary_ (20000 100000 500000);
    array valuesString[4] $10 _temporary_ ("<20k " "20k-100k" "100k-500k" ">500k");

    salary_string  = cut2string(salary ,cutoffs,valuesString);
run;


来源:https://stackoverflow.com/questions/35397018/categorizing-variabels-in-sas-using-a-range-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!