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

Hello team,

This code doesn't produce the result that is needed:

data countrydata;

input EmployeeID Province$ PostalCode$ Country$;

cards;

737874 Ontario N0H1G0 CAD

876098 Quebec H4S1Y9 CAD

456231 Alberta T0J1K0 cad

874572 Manitoba R0K0V0 CAD

680562 Ontario K0K1A0 cad

737874 Ontario N0H1G0 CAD

874598 Quebec H4S1Y9 USA

455731 Alberta T0J1K0 cad

874372 Manitoba R0K0V0 CAD

680462 Ontario K0K1A0 cad

737374 Ontario N0H1G0 CAD

876298 Quebec H4S1Y9 CAD

456431 Alberta T0J1K0 USA

874572 Manitoba R0K0V0 CAD

6430362 Ontario K0K1A0 cad

737274 Ontario N0H1G0 CAD

873098 Quebec H4S1Y9 CAD

465633 Alberta T0J1K0 cad

874357 Manitoba R0K0V0 CAD

6834562 Ontario K0K1A0 cad

run;

data countrydata2;
set countrydata;
where Country = upcase(cad);
run;

Proc sort data= countrydata2 out = subsetme nodupkey;
by ascending Country descending PostalCode;
run;

If we want to use ascending, can we do that?

where Country = upcase(cad); doesn't filter the data.

Any help is greatly appreciated.

Regards,

BlueBlue

Blue Blue
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

There are two issues with your code.

Issue 1: You want to filter on a value in Country and not on a value stored in another variable named cad. The SAS log tells you that there isn't a variable CAD.

where Country = upcase(cad);

The syntax needs to be: where upcase(country)='CAD';

 

Issue 2: With Proc Sort data gets sorted in ascending order if you don't use keyword DESC / DESCENDING. The keyword ASCENDING doesn't exist with Proc Sort - it's not listed in the SAS docu so it's not available.

 

Here your code with the fixes.

data countrydata;
  input EmployeeID Province$ PostalCode$ Country$;
  cards;
737874 Ontario N0H1G0 CAD
876098 Quebec H4S1Y9 CAD
456231 Alberta T0J1K0 cad
874572 Manitoba R0K0V0 CAD
680562 Ontario K0K1A0 cad
737874 Ontario N0H1G0 CAD
874598 Quebec H4S1Y9 USA
455731 Alberta T0J1K0 cad
874372 Manitoba R0K0V0 CAD
680462 Ontario K0K1A0 cad
737374 Ontario N0H1G0 CAD
876298 Quebec H4S1Y9 CAD
456431 Alberta T0J1K0 USA
874572 Manitoba R0K0V0 CAD
6430362 Ontario K0K1A0 cad
737274 Ontario N0H1G0 CAD
873098 Quebec H4S1Y9 CAD
465633 Alberta T0J1K0 cad
874357 Manitoba R0K0V0 CAD
6834562 Ontario K0K1A0 cad
;

data countrydata2;
  set countrydata;
  where upcase(Country) = 'CAD';
run;

Proc sort data= countrydata2 out = subsetme nodupkey;
  by Country descending PostalCode;
run;

 

View solution in original post

1 REPLY 1
Patrick
Opal | Level 21

There are two issues with your code.

Issue 1: You want to filter on a value in Country and not on a value stored in another variable named cad. The SAS log tells you that there isn't a variable CAD.

where Country = upcase(cad);

The syntax needs to be: where upcase(country)='CAD';

 

Issue 2: With Proc Sort data gets sorted in ascending order if you don't use keyword DESC / DESCENDING. The keyword ASCENDING doesn't exist with Proc Sort - it's not listed in the SAS docu so it's not available.

 

Here your code with the fixes.

data countrydata;
  input EmployeeID Province$ PostalCode$ Country$;
  cards;
737874 Ontario N0H1G0 CAD
876098 Quebec H4S1Y9 CAD
456231 Alberta T0J1K0 cad
874572 Manitoba R0K0V0 CAD
680562 Ontario K0K1A0 cad
737874 Ontario N0H1G0 CAD
874598 Quebec H4S1Y9 USA
455731 Alberta T0J1K0 cad
874372 Manitoba R0K0V0 CAD
680462 Ontario K0K1A0 cad
737374 Ontario N0H1G0 CAD
876298 Quebec H4S1Y9 CAD
456431 Alberta T0J1K0 USA
874572 Manitoba R0K0V0 CAD
6430362 Ontario K0K1A0 cad
737274 Ontario N0H1G0 CAD
873098 Quebec H4S1Y9 CAD
465633 Alberta T0J1K0 cad
874357 Manitoba R0K0V0 CAD
6834562 Ontario K0K1A0 cad
;

data countrydata2;
  set countrydata;
  where upcase(Country) = 'CAD';
run;

Proc sort data= countrydata2 out = subsetme nodupkey;
  by Country descending PostalCode;
run;

 

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
  • 1 reply
  • 829 views
  • 1 like
  • 2 in conversation