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

I have rows of news which I need to give codes to based on individuals in the news. I am trying to do the following thing:

 

data zzz;

set yyy;

if there is "% Bernanke%" in Event, then there is a new column SF1=1; else SF1=0;

if there is "% Rosengren%" in Event, then there is a new column SF2=1; else SF2=0;

if there is "% Paulson%" in Event, then there is a new column SF3=1; else SF3=0;

… … … … … … … … … … … … … … … … … … … … … … … … … … … … …

run;

 

Also, I would like to set the length of the new variables (columns) SF1, SF2, SF3... to $5. How can I set the length for all the new columns at the beginning except writing: length SF1 $5.; for each new variable?

 

Regards.

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisBrooks
Ammonite | Level 13

To reference multiple variables with the same prefix and a numeric suffix you can use a hyphen as in the example below. You can also use the find function to search a string for a substring.

 

Here's an example using the SASHELP.CARS data set

 

data out;
	length sf1-sf3 $5;
	set sashelp.cars;
	if find(model,"convertible") then sf1=1;
		else sf1=0;
	if find(model,"manual") then sf2=1;
		else sf2=0;
	if find(model,"auto") then sf3=1;
		else sf3=0;	
run;
	

View solution in original post

3 REPLIES 3
ChrisBrooks
Ammonite | Level 13

To reference multiple variables with the same prefix and a numeric suffix you can use a hyphen as in the example below. You can also use the find function to search a string for a substring.

 

Here's an example using the SASHELP.CARS data set

 

data out;
	length sf1-sf3 $5;
	set sashelp.cars;
	if find(model,"convertible") then sf1=1;
		else sf1=0;
	if find(model,"manual") then sf2=1;
		else sf2=0;
	if find(model,"auto") then sf3=1;
		else sf3=0;	
run;
	
Astounding
PROC Star

If you define a variable as character with a length of 5, and then attempt to store a number in it, do you have any idea of what the result will be?

d6k5d3
Pyrite | Level 9
So stupid of me! I realized my mistake. Thank you 😄

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

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 888 views
  • 1 like
  • 3 in conversation