Trivial question that i don't understand. The documentation for a data file i'm preparing specifies that subject_id be a 20 character string (text) variable. The values that i am loading into that variable are 3 digit numbers. I do this: subject_id=put(record_id, 20.);. when i examine the dataset in viewtable, data values appear to be maybe somewhat centered in that 20 character field.
I am also required to define a 20 character text variable for sex and load single character data values (M or F). I do this:
length sex $ 20;
sex=tsex;
Values for this variable display against the left field boundary, i.e., character column 1.
I then read this file, after saving it, into spss and see that subject_id data values display in columns 17-20 and sex data values display in columns 1-3.
Perhaps it doesn't matter but I'd like to understand why the differences and how to make them similar.
Thank you, Gene Maguin
Leading blanks definitely matter. : )
When you use the PUT function to convert a numeric value to character and specify the format as 20. you are asking SAS to give you have value that is 20 characters long. So if you have 3 digit number, you will have 17 blanks followed by 3 digits.
In SAS, leading blanks can cause lots of confusion, because they are part of the value:
data want ;
record_id=123 ;
subject_id=put(record_id,20.) ;
if subject_id='123' then put "Not found! ";
if subject_id=' 123' then put "leading blanks!" ;
run ;
You can add the -l (that's l for left) option on the format to left-align the value:
data a ;
record_id=123 ;
subject_id=put(record_id,20. -l) ;
if subject_id='123' then put "no leading blanks when you use -l" ;
run ;
Don't look at printouts made using ODS as it will distort things. In addition to using proportional fonts it will also remove leading spaces.
Most likely the difference is because TSEX as already character, and so did not have any leading spaces.
The numeric format, such as the 20. you used, will right align the values.
Unless there is some need to display the numbers in a particular style (perhaps the numbers are DATE values for example) then I would just use the CATS() function instead of PUT(). That also has the advantage of working the same with either numbers or strings as input.
length subject_id sex $20 ;
subject_id=cats(record_id);
sex=cats(tsex);
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.