Help using Base SAS procedures

FORMATS

Reply
Super Contributor
Posts: 1,041

FORMATS

Hi ,

I have a numeric format for numeric variable;

but I am applying the same format on a charcecter variable (CODE)in the datastep.

could you help me figure out where I went wrong??

Thanks

proc format;
value MSDRG
001-017, 020-042, 113-117, 129-139,="1"
other="0";
run;

data elective_surgical;
set final_with_MSDRG;
where put(Code, MSDRG.)="1";
run;

Super User
Posts: 19,789

Re: FORMATS

Posted in reply to robertrao

That depends, what's not working?

The following works for me. I'm confused by your format though, 001-017 is the same as 1-17, if you have leading 0's you likely have a character variable not a numeric variable.

data have;

do code=1 to 200;

  output;

end;

run;

proc format;

value MSDRG

001-017, 020-042, 113-117, 129-139="1"

other="0";

run;

data elective_surgical;

set have;

where put(Code, MSDRG.)="1";

run;

Super User
Posts: 11,343

Re: FORMATS

Posted in reply to robertrao

You should have a separate charater value format where the value statement is Value $msdrg and use Put(code,$msdrg).

Character and numeric formats are different for a number of reasons.

Super User
Posts: 10,028

Re: FORMATS

Posted in reply to robertrao

If Code is character type, then you need transform it into numeric type ,since your format is numeric type.

data have;
do i=1 to 200;
  code=put(i,best8. -l);output;
end;
drop i;
run;
 
proc format;
value MSDRG
001-017, 020-042, 113-117, 129-139="1"
other="0";
run;
 
 
data elective_surgical;
set have;
where put(input(Code,best8.), MSDRG.)="1";
run;

Xia Keshan

Super Contributor
Posts: 297

Re: FORMATS

Posted in reply to robertrao

Apart from a redundant comma in your proc format your code works fine for me.

If you want to see the leading zeros in your initial numeric values, simply use the z3. format as below.

PROC FORMAT;

VALUE MSDRG

001-017, 020-042, 113-117, 129-139="1"

OTHER="0";

RUN;

DATA FINAL_WITH_MSDRG;

FORMAT CODE Z3.;

CODE = 001;

OUTPUT;

CODE = 140;

OUTPUT;

RUN;

DATA ELECTIVE_SURGICAL;

SET FINAL_WITH_MSDRG;

WHERE PUT(CODE, MSDRG.)="1";

RUN;

Super Contributor
Posts: 297

Re: FORMATS

Posted in reply to robertrao

If you want to format a character variable to return another character value you can use the PUTC function.

PROC FORMAT;

VALUE $MSDRG

"001"-"017"="1"

OTHER="0";

RUN;

DATA FINAL_WITH_MSDRG;

FORMAT CODE;

CODE = "001";

OUTPUT;

CODE = "140";

OUTPUT;

RUN;

DATA ELECTIVE_SURGICAL;

SET FINAL_WITH_MSDRG;

ATTRIB STUFF FORMAT=$1.;

STUFF=PUTC(CODE, "$MSDRG.")="1";

RUN;

Ask a Question
Discussion stats
  • 5 replies
  • 973 views
  • 0 likes
  • 5 in conversation