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;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.