DATA Step, Macro, Functions and more

format

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

format

hello,

 

I have a numeric field called DAYSTOADMIT(contains values like 0, 1 ,33, 24 etc etc)

I am trying to apply formats to assign 1 to those that have a missing value by assigning them a value of 1 so i can summarize later using tabulate.

.

I used the following format and from the resultant dataset when i say "where DAYSTOADMIT ne 0" I AM GETTING 0 pulled up . It is surprising to me..

can someone explain why this is happening???

 

 

Proc format library=work;

invalue ADMIT

. = 1

other=0

;

run;

 

data want;

set have;

informat DAYSTOADMIT Admit.;

run;


Accepted Solutions
Solution
‎05-23-2017 04:58 AM
Super User
Posts: 6,931

Re: format

Then do a datastep like this

data want (keep=c_all c_miss percentage);
set have end=done;
retain c_all 0 c_miss 0;
c_all + 1;
c_miss + (daystoadmit = .);
if done
then do;
  percentage = c_miss / c_all;
  put percentage percent8.2;
  call symput('percentage',put(percentage,percent8.2));
  call symput('percentage_r',put(percentage * 100,best.));
  output;
end;
run;

%put &percentage;
%put &percentage_r;

You'll see te values in the log, you have them in a dataset and in macro variables.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 6,931

Re: format

Assigning a format or informat NEVER changes the value of a variable.

So the best option is

data want;
set have;
if daystoadmit = . then daystoadmit = 1;
run;

And why don't you simply use

where daystoadmit = .;

?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super Contributor
Posts: 1,040

Re: format

Thanks Kurt.

I think we need to use the following:Is that true when i want to get the total number of records later?

 

data want;
set have;
if daystoadmit = . then daystoadmit = 1; else daystoadmit =0;
run;

Super User
Posts: 6,931

Re: format

If you just want to know the number of missings, do

proc sql;
select count(*) from have where daystoadmit is missing;
quit;

Or use a proc freq on daystoadmit and extract the observation for missing.

So you don't have to change the value at all, and preserve the original information.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super Contributor
Posts: 1,040

Re: format

Thanks again. Actually I need to do more than that. I had the same thing posted a few days ago and i got good response/help.

i am doing the :

 

count of missing/Total records*100

Solution
‎05-23-2017 04:58 AM
Super User
Posts: 6,931

Re: format

Then do a datastep like this

data want (keep=c_all c_miss percentage);
set have end=done;
retain c_all 0 c_miss 0;
c_all + 1;
c_miss + (daystoadmit = .);
if done
then do;
  percentage = c_miss / c_all;
  put percentage percent8.2;
  call symput('percentage',put(percentage,percent8.2));
  call symput('percentage_r',put(percentage * 100,best.));
  output;
end;
run;

%put &percentage;
%put &percentage_r;

You'll see te values in the log, you have them in a dataset and in macro variables.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 168 views
  • 0 likes
  • 2 in conversation