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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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