I used proc format to format a variables, want to change the codes (let's say 10 codes) to 3 categories, A B C;
proc format;
value change
1 = 'A'
2= 'B'
....;
run;
data data1;
set data;
format codes change.;
run;
proc sql;
create table example as
select counts(var), sum(var)
from table
group by codes;
run;
It seems that proc format only cover the codes with the format, because it's still numeric variable and
when you group by, you still get 10 groups (corresponding to 10 codes), not three groups.
How to group by the three categories? thanks.
Maybe this is what you want?
proc format;
value change
1,3,8 = 'A'
2,7= 'B'
4,5,6,9,10='C';
run;
If not, then please provide a more clear example that shows the mapping of the 10 codes into three groups.
But I don't think SQL will recognize these formatted categories, I think SQL will use the unformatted categories. Now, you can do the formatting inside of SQL, or use PROC SUMMARY to get counts and sum by the formatted levels of codes.
data have2;
set have1;
format codes change.;
run;
proc summary data=have2;
class codes;
var var;
output out=_stats_ sum=sumvar n=nvar;
run;
Thanks Paige for your input.
I want to hardcode or generated another column to group by the three categories A B C. I like sql more than sas.
I just tested the put(var, format), it's good.
You can get PROC SUMMARY to create/convert a numeric class variable to character using the MLF CLASS statement option.
proc format;
value change(notsorted)
1,3,8 = 'A'
2,7= 'B'
4,5,6,9,10='C';
run;
data have;
do code = 1 to 10;
y=ranuni(1);
output;
end;
run;
proc summary data=have nway;
class code / mlf order=data preloadfmt;
format code change.;
var y;
output out=_stats_ sum=sumvar n=nvar;
run;
proc contents varnum;
run;
proc print;
run;
Guru @data_null__ been following you for long and the time has come yet again to knock at your intelligence door to bother.
I am unable to understand notsorted in proc format viz. the intuition behind it. When and if you have a moment, some more notes plz.
and as always thank you * infinite!
I suspect it's related to the last note here:
stores values or ranges in the order in which you define them. If you do not specify NOTSORTED, then values or ranges are stored in sorted order by default, and SAS uses a binary searching algorithm to locate the range that a particular value falls into. If you specify NOTSORTED, then SAS searches each range in the order in which you define them until a match is found.
Use NOTSORTED if one of the following is true:
Thank you Reeza & DN for the responses. Looks like I need some real scenario to get the mind to think along those lines so that I grip the intutitive sense. Nonetheless, I have added to my notes
@novinosrin wrote:
Guru @data_null__ been following you for long and the time has come yet again to knock at your intelligence door to bother.
I am unable to understand notsorted in proc format viz. the intuition behind it. When and if you have a moment, some more notes plz.
and as always thank you * infinite!
@novinosrin I used a few options that are not actually necessary to the point I was trying to make. I think one of @pensarchem wants was to convert the variable CODE to character with the formatted value. The conversion to character is accomplished with the CLASS statement option MLF.
Guru @data_null__ Thank you & Sorry for the late acknowledgement as I was away. Okay, the conversion part is now clear and pretty well comprehended. The institution to notsorted in format is something I am understanding but not practically appreciating it yet. I will have to wait on a real scenario as you know I am outside of clinical domain
@novinosrin you're welcome and thank you. I think these links to the sample library may be helpful examples of NOTSORTED ORDER=DATA and MULTILABEL formats.
http://support.sas.com/kb/12/904.html
http://support.sas.com/kb/45/458.html
http://support.sas.com/kb/23/846.html
http://support.sas.com/kb/23/847.html
Thanks Reeza. It's good. put(var, change.) works beautifully.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.