BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aanan1417
Quartz | Level 8

data have;

input state $ income;

datalines;

delhi             1000

delhi             3000

chandigarh   4000

chandigarh   3000

patna             300

delhi               400

;

run;

 

data want;

input state $ income;

datalines;

delhi             4400

chandigarh   7000

;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
SASJedi
Ammonite | Level 13

As written, the length of the state variable in your have and want datasets will default to 8 - too small to hold all the values. Try this:

data have;
length state $ 10;
input state $ income;
datalines;
delhi             1000
delhi             3000
chandigarh   4000
chandigarh   3000
patna             300
delhi               400
;
run;

 

data want;
length state $ 10;
input state $ income;
datalines;
chandigarh   7000
delhi             4400
;

run;

proc sql;
/*create table want as */
select state, sum(income)
   from have
   where lowcase(state) in ('delhi','chandigarh')
   group by state
;
quit;

May the SAS be with you 😉

Check out my Jedi SAS Tricks for SAS Users

View solution in original post

2 REPLIES 2
SASJedi
Ammonite | Level 13

As written, the length of the state variable in your have and want datasets will default to 8 - too small to hold all the values. Try this:

data have;
length state $ 10;
input state $ income;
datalines;
delhi             1000
delhi             3000
chandigarh   4000
chandigarh   3000
patna             300
delhi               400
;
run;

 

data want;
length state $ 10;
input state $ income;
datalines;
chandigarh   7000
delhi             4400
;

run;

proc sql;
/*create table want as */
select state, sum(income)
   from have
   where lowcase(state) in ('delhi','chandigarh')
   group by state
;
quit;

May the SAS be with you 😉

Check out my Jedi SAS Tricks for SAS Users

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 823 views
  • 1 like
  • 3 in conversation