BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14

Hello

X_char is a char var.

I run following code and expext that resulted VAR (W) be char with values-1,2,3,4

My question- Why VAR W is numeric???

 

proc format ;
value $F1_High_Risk_ithalv
'High_50-100%-Med_0-50%-Low_0-50%',
'High_50-100%-Low_0-50%',
'High_100%',
'High_50-100%-Med_0-50%',
'High_0-50%-Med_50-100%-Low_0-50%',
'High_0-50%-Med_50-100%',
'Med_50-100%-Low_0-50%',
'Med_100%'
=1

'High_0-50%-Med_0-50%-Low_50-100%',
'Med_0-50%-Low_50-100%',
'High_0-50%-Low_50-100%'
=2

'High_0-50%-Med_0-50%-Low_0-50%',
'ithalv=0'
=3

'Low_100%'
=4
;
Run;

W=put(compress(X_CHAR),$F1_High_Risk_ithalv.);
1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

It's not numeric!

data test;
x_char='High_50-100%-Med_0-50%-Low_0-50%';
W=put(compress(X_CHAR),$F1_High_Risk_ithalv.);
run;
proc contents;
run;

yabwon_0-1724833108861.png

SAS just let's you to be a "lazy typer".

Even though you provide values 1,2,3 without quotes, since you are defining character format, the values 1,2,3, are interpreted as they were "1", "2", "3".

Of course for better readability adding "" would be good idea.

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

3 REPLIES 3
Ksharp
Super User
You need put QUOTE around 1,2,3 . Like:
= '1'
....
='2'
yabwon
Onyx | Level 15

It's not numeric!

data test;
x_char='High_50-100%-Med_0-50%-Low_0-50%';
W=put(compress(X_CHAR),$F1_High_Risk_ithalv.);
run;
proc contents;
run;

yabwon_0-1724833108861.png

SAS just let's you to be a "lazy typer".

Even though you provide values 1,2,3 without quotes, since you are defining character format, the values 1,2,3, are interpreted as they were "1", "2", "3".

Of course for better readability adding "" would be good idea.

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

Because FORMATs convert VALUES into TEXT.  The result of using PUT() is ALWAYS going to be character.

 

If you want to create a numeric value you need to use an INFORMAT with the INPUT() function.  INFORMATs convert TEXT into VALUES.  

 

proc format ;
invalue F1_High_Risk_ithalv 
 'High_50-100%-Med_0-50%-Low_0-50%'
,'High_50-100%-Low_0-50%'
,'High_100%'
,'High_50-100%-Med_0-50%'
,'High_0-50%-Med_50-100%-Low_0-50%'
,'High_0-50%-Med_50-100%'
,'Med_50-100%-Low_0-50%'
,'Med_100%'
=1

 'High_0-50%-Med_0-50%-Low_50-100%'
,'Med_0-50%-Low_50-100%'
,'High_0-50%-Low_50-100%'
=2

 'High_0-50%-Med_0-50%-Low_0-50%'
,'ithalv=0'
=3

'Low_100%'
=4
;
run;

data test ;
  input x_char $80.;
  W=input(compress(X_CHAR),F1_High_Risk_ithalv.);
cards;
Low_100%
Med_100%
High_0-50%-Low_50-100%
ithalv=0
;;;;

proc print;
run;

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 932 views
  • 2 likes
  • 4 in conversation