Hi, this might sound stupid, but i have been looking for the answer for last few hours, but I still can't figure this out.
The following code shows how I define my 'agegroup' variable:
if i in (1, 5) then agegroup = '<30';
else if i in (2, 6) then agegroup = '31-45';
else if i in (3, 7) then agegroup = '46-60';
else agegroup = '60+';But my sas output eliminates the number after the dash:
I thought this might be a problem about variable type, but I am not sure anymore. I also tried to replace the single quote with double quote, but it isn't working.
If you don't specify a length for a character variable it takes as its length the length of the first value assigned to it. In your case there must be a value of either 1 or 5 as the first value in the data so agegroup has a length of 3 ('<30').
You can correct this by adding
length agegroup $5;
before your set statement
If you don't specify a length for a character variable it takes as its length the length of the first value assigned to it. In your case there must be a value of either 1 or 5 as the first value in the data so agegroup has a length of 3 ('<30').
You can correct this by adding
length agegroup $5;
before your set statement
"In your case there must be a value of either 1 or 5 as the first value in the data so agegroup has a length of 3 "
Variables get defined during the compilation phase and before data step iteration so what's in the data doesn't matter. The full reason for a length of 3 is, that this is the length of the first value assignment in the code: then agegroup = '<30'
@ChrisBrooks gave you the explanation to your problem.
Instead of creating a new variable, you can use a format :
proc format;
value agegroup
1,5='<30'
2,6='31-45'
3,7='46-60'
other='60+'
;
run;
proc print data=have;
format i agegroup.;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.