BookmarkSubscribeRSS Feed
IvanDiego
Calcite | Level 5

I'm trying to input some data into a table that is devided up into 3 different categories: Small, medium and large.

The problem is when I run/submit the code, I have only the word "Mediu" instead of "medium" pop up in my categories box of my table.

What do I need to do for the word "medium" to appear on my table?

I'm currently running: 

Data two;
 Set 1;
 If greenberet1 < 40 then Battalion='Small';
 Else if greenberet1 < 100 then Battalion = 'Medium';
 Else Battalion = 'Large'; 
Run; 

Proc freq; Tables Battalion*Advanced; Run;
4 REPLIES 4
Rick_SAS
SAS Super FREQ

 Data two;

length Battalion $6;

Set 1;

If greenberet1 < 40 then ...

ChrisNZ
Tourmaline | Level 20
Please change the title of your post to something meaningful, such as: Why is this string value truncated?
ballardw
Super User

@IvanDiego wrote:

I'm trying to input some data into a table that is devided up into 3 different categories: Small, medium and large.

The problem is when I run/submit the code, I have only the word "Mediu" instead of "medium" pop up in my categories box of my table.

What do I need to do for the word "medium" to appear on my table?

I'm currently running: Data two; Set 1; If greenberet1 < 40 then Battalion='Small'; Else if greenberet1 < 100 then Battalion = 'Medium'; Else Battalion = 'Large'; Run; Proc freq; Tables Battalion*Advanced; Run;


You may also want to consider custom formats such as:

proc format library=work;
value greenberet
0  -< 40 = 'Small'
40 -<100 = 'Medium'
100- high= 'Large'
;

proc freq data=one;
   tables greenberet1*advanced;
   format greenberet1 greenberet. ;
run;

When a display value depends on a single variable, in this case Greenberet1, a format is often a good choice.

Reasons: The logic code is removed from a data step which in the case of multiple groups (following in some of your vocabulary think: fire team, squad, platoon, company, battalion, regiment, brigade, division, corps if you wanted a single number of men to be an equivalent that is bit more code).

Second if you decide that you need to have a different grouping for some purpose you can create a different format, or modify the code for the existing format, and the report, analysis or graphing procedure will use the groups created by the format without having to add yet another variable.

 

Changing the format to:

proc format library=work;
value greenberet
0  -< 50 = 'Small'
50 -<100 = 'Medium'
100- high= 'Large'
;
run;

proc freq data=one;
   tables greenberet1*advanced;
   format greenberet1 greenberet. ;
run;

will get a different result and no changes to the data set are needed OR the Proc Freq code.

 

Be default the entire text label associated with the format will be displayed. But you could modify that by specifying a different display length such as

format greenberet1 greenberet1. ; displays one character from the format: S, M or L

format greenberet1 greenberet3. ; displays three characters from the format: Sma Med Lar

The ability to specify the display width is why you can't use a digit as the last character in a format name (the Value statement).

 

I have some data that reports on more than 15 different age groups. I really don't want to keep track of that many different variables that are similar. Plus I never know when one of my users wants to see "what happens if we shift the age boundary from 17 to 19".

 

You do have to make sure the format is available when needed though by either running the format code in each session or placing the format in a location your SAS system FMTSEARCH option sets for finding formats.

Tom
Super User Tom
Super User

Since you never defined BATTALION SAS had to guess how you wanted it defined based on how you first used it.

Since the first usage is setting it to a five character string it defined the variable to have length of 5.

Define the variable before giving it values.

length battalion $8;

Or make sure that the first place you use it in the code you is in a way that SAS will create it with a length that is as long or longer than any value you want it to hold.

 If greenberet1 < 40 then Battalion='Small         ';

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 743 views
  • 0 likes
  • 5 in conversation