DATA Step, Macro, Functions and more

How to print a variable containing capital letter X?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

How to print a variable containing capital letter X?

How do I print a variable containing capital letter X?

I have no idea how to write the code, please help. 


Accepted Solutions
Solution
4 weeks ago
PROC Star
Posts: 8,114

Re: How to print a variable containing capital letter X?

Assuming respop72015 represents the population in 2015, then here is one way to do it:

data have;
  infile '/folders/myfolders/USPOP.csv' dlm=',' dsd lrecl=200 firstobs=2;
  informat GEO_id $12.;
  informat GEO_display_label $40.;
  input GEO_id GEO_display_label respop72010-respop72015;
run;

proc print data=have (where=(index(GEO_display_label,'C') and
                      GEO_id ne '0400000US11'));
  var GEO_display_label respop72015;
run;

Art, CEO, AnalystFinder.com

 

View solution in original post


All Replies
Trusted Advisor
Posts: 1,826

Re: How to print a variable containing capital letter X?

Having a char type variable you can use either of next functions:

-  UPCASE will change all charcters into upper case (capital) letters

-  LOCASE will change all characters into low case letters

 

syntax:  var = upcase(var);  same with locase.

 

You can change 1st letter only by:

     substr(var,1,1) = upcase(substr(var,1,1));

Occasional Contributor
Posts: 5

Re: How to print a variable containing capital letter X?

How do I print it if the letter I selected is not in the first position?
PROC Star
Posts: 1,328

Re: How to print a variable containing capital letter X?


@elaine00wrote:

How do I print a variable containing capital letter X?

I have no idea how to write the code, please help. 


 

 

Do you mean 

 

data example;

length var $50;

var='Xgjhhkj';

output;

var='iuyuiuioX';

output;

var='trt';

output;

run;



data wanted_output;

set example;

if index(var,'X')>0;
 put var=;

run;

 

Occasional Contributor
Posts: 5

Re: How to print a variable containing capital letter X?

Posted in reply to novinosrin
It is a dataset, I am supposed to print all the obs that containing the capital letter X of a variable (there are many variable and obs).
PROC Star
Posts: 1,328

Re: How to print a variable containing capital letter X?

Ok, Can you please post a sample of your dataset like 10 obs/vars with values and your expected output. Also, when you say "print" , please let the community know whether you want the output in a sas dataset or ods or printed values in the log. This will help the community to not assume your requirement. Thank you!

Occasional Contributor
Posts: 5

Re: How to print a variable containing capital letter X?

Posted in reply to novinosrin

Thank you. The question says, Print the population of the states containing the letter C (Capital C, case sensitive) in their GEO_display_label. and I attached the csv file

Solution
4 weeks ago
PROC Star
Posts: 8,114

Re: How to print a variable containing capital letter X?

Assuming respop72015 represents the population in 2015, then here is one way to do it:

data have;
  infile '/folders/myfolders/USPOP.csv' dlm=',' dsd lrecl=200 firstobs=2;
  informat GEO_id $12.;
  informat GEO_display_label $40.;
  input GEO_id GEO_display_label respop72010-respop72015;
run;

proc print data=have (where=(index(GEO_display_label,'C') and
                      GEO_id ne '0400000US11'));
  var GEO_display_label respop72015;
run;

Art, CEO, AnalystFinder.com

 

Occasional Contributor
Posts: 5

Re: How to print a variable containing capital letter X?

Thank you! It really helps!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 252 views
  • 0 likes
  • 4 in conversation