BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8

I read the doc but dont understand as the example is not complete. I try the code attached,

data pf_lemon;
input scorefmt $;
datalines;
low-539
540-559
560-579   
run;

proc format data=pf_lemon;
value scorefmt
low-539 ="apple"
540-559="540<=SCORE<560"
560-579="560<=SCORE<580";
run;

proc print data=pf_lemon;
run;

how can I make it work?

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Three problems:

 

  1. Since SCOREFMT is a character variable, you have to create a character format, which would have a name that begins with a $. You created a numeric format which won't work.
  2. There is no DATA= in PROC FORMAT
  3. You have to associate the character format $scorefmt with the variable SCOREFMT, this is done via a FORMAT statement in a PROC or in a DATA step.

 

proc format;
value $scorefmt
'low-539' ="apple"
'540-559'="540<=SCORE<560"
'560-579'="560<=SCORE<580";
run;

proc print data=pf_lemon;
format scorefmt $scorefmt.;
run;

 

--
Paige Miller
HeatherNewton
Quartz | Level 8

thank you for your guidance

I tried as you suggested, it works generallly except for the line with score low-539, pls see result below

 

HeatherNewton_0-1646969096045.png

  input up down scorefmt $ sub_product  $;
datalines;
2 5 low-539 apple
3 2 540-559 lemon 
4 0 560-579 melon
run;

proc format;
value $scorefmt
low-539="SCORE<540"
540-559="540<=SCORE<560"
560-579="560<=score<580";
RUN;

proc print data=lemon;
format scorefmt $scorefmt.;
run;
ballardw
Super User

Please post a link to the "example" source. If that is an example of what some site is using then it is either a very bad site, due to the number of errors that "example" generate, or you are skipping a lot of the actual example.

 

And what do you mean by "make it work"??? We have no idea what you would expect the result to be if or when it does "work".

 


@HeatherNewton wrote:

I read the doc but dont understand as the example is not complete. I try the code attached,

data pf_lemon;
input scorefmt $;
datalines;
low-539
540-559
560-579   
run;

proc format data=pf_lemon;
value scorefmt
low-539 ="apple"
540-559="540<=SCORE<560"
560-579="560<=SCORE<580";
run;

proc print data=pf_lemon;
run;

how can I make it work?


 

Kurt_Bremser
Super User

Your format is designed to combine single numeric values into ranges, so you should feed it such.

data pf_lemon;
input score;
datalines;
520
550
565
;

And then

proc print data=pf_lemon;
var score;
format score scorefmt.;
run;

Such range formats become very valuable when used with statistic procedures (FREQ, MEANS, TABULATE), as these will use the formatted values for grouping.

 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

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
  • 4 replies
  • 415 views
  • 0 likes
  • 4 in conversation