Issue with proc format/put statement using if/then statements

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

Issue with proc format/put statement using if/then statements

[ Edited ]

I'm having an issue when I try to use a put statement to create a new variable. My proc format seems to be the problem point, as when I print the data set, the format does not seem to take. That is, I am trying to create a new variable called "Continent2" that would change any continent not named "Europe" to "Non-Europe," but when the data prints, there is no change in name between the two variables Continent and Continent2. Should I need to include a format statement in my print procedure as well?

 

data IMO_data;
	infile "C:\Users\Alicia\Desktop\BIOS623Fa16_02_A04_IMO_Data.txt";
	input @1 ID $5. @7 Host_Country $14. @22 Continent $13. @22 Continent $13. @36 
		Open_Date date9. @46 Close_Date date9. @57 Num_Men 3.0 @65 Num_Women 2.0;
	format Open_Date mmddyy8. Close_Date mmddyy8.;
run;

proc print data=IMO_data;
run;

proc format ;
	value $Continent_fmt if Continent ~="Europe" then Continent="Non-Europe";
run;

data IMO_data;
	set IMO_data;
	Continent2=put(Continent, $Continent_fmt.);
run;

proc print data=IMO_data;
	var ID Continent Continent2;
run;

Accepted Solutions
Solution
‎11-27-2016 07:19 PM
Trusted Advisor
Posts: 1,370

Re: Issue with proc format/put statement using if/then statements

IF can be used only in a data step, not in a PROC.

You code should be:

 

data IMO_data;
infile "C:\Users\Alicia\Desktop\BIOS623Fa16_02_A04_IMO_Data.txt";
input @1 ID $5. @7 Host_Country $14. @22 Continent $13. @36 Open_Date date9. 
@46 Close_Date date9. @57 Num_Men 3.0 @65 Num_Women 2.0; format Open_Date mmddyy8. Close_Date mmddyy8.; run; proc print data= IMO_data; run; data IMO_data; set IMO_data;
if Continent ~= "Europe" then Continent = "Non-Europe";
run;
proc print data= IMO_data;
var ID Continent ; /* you don't have continent2 in your data */
run;

 

 

 

View solution in original post


All Replies
Super User
Posts: 17,784

Re: Issue with proc format/put statement using if/then statements

Check your log. What does it say regarding your PROC FORMAT? 

Occasional Contributor
Posts: 13

Re: Issue with proc format/put statement using if/then statements

No errors or warnings to give insight into how to correct the problem:

 

11 proc format;
12 value $Continent_fmt
13 if Continent~= "Europe" then Continent = "Non-Europe"
14 ;
NOTE: Format $CONTINENT_FMT has been output.
15 run;

NOTE: PROCEDURE FORMAT used (Total process time):
real time 0.06 seconds
cpu time

Super User
Posts: 17,784

Re: Issue with proc format/put statement using if/then statements

Weird. 

 

See ths this paper on how to create a format. 

 

http://www2.sas.com/proceedings/sugi30/001-30.pdf

Super User
Posts: 17,784

Re: Issue with proc format/put statement using if/then statements

The correct PROC FORMAT would have been:

 

proc format ;
	value $Continent_fmt 
            "Europe"="Europe"
               other="Non-Europe";
run;
Super User
Posts: 10,483

Re: Issue with proc format/put statement using if/then statements

Run this code and see if you get a clue as to what happened:

data junk;
   x="if Continent~=Europe then Continent";output;
   x="other text";output;
run;

proc print data=junk;
  format x $Continent_fmt.;
run;

Or use the CNTLOUT option on  the proc format statement and examine the resulting data set.

 

Solution
‎11-27-2016 07:19 PM
Trusted Advisor
Posts: 1,370

Re: Issue with proc format/put statement using if/then statements

IF can be used only in a data step, not in a PROC.

You code should be:

 

data IMO_data;
infile "C:\Users\Alicia\Desktop\BIOS623Fa16_02_A04_IMO_Data.txt";
input @1 ID $5. @7 Host_Country $14. @22 Continent $13. @36 Open_Date date9. 
@46 Close_Date date9. @57 Num_Men 3.0 @65 Num_Women 2.0; format Open_Date mmddyy8. Close_Date mmddyy8.; run; proc print data= IMO_data; run; data IMO_data; set IMO_data;
if Continent ~= "Europe" then Continent = "Non-Europe";
run;
proc print data= IMO_data;
var ID Continent ; /* you don't have continent2 in your data */
run;

 

 

 

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 243 views
  • 0 likes
  • 4 in conversation