BookmarkSubscribeRSS Feed
robertrao
Quartz | Level 8

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;

5 REPLIES 5
Reeza
Super User

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;

ballardw
Super User

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.

Ksharp
Super User

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

Scott_Mitchell
Quartz | Level 8

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;

Scott_Mitchell
Quartz | Level 8

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2871 views
  • 0 likes
  • 5 in conversation