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.
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;
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;
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?
Nearly 200 sessions are now available on demand in the Innovate Hub.
Watch Now →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.