BookmarkSubscribeRSS Feed
hmlong25
Obsidian | Level 7

This is from the DATA Manipulation Course 9.4 Base Programming.

Please explain the use of the put function in this activity.

 

Open p204a03.sas from the activities folder and perform the following tasks:

 

  • Activity 4.03                                          *;
    *  1) Review the PROC FORMAT step that creates the        *;
    *     $REGION format that assigns basin codes into        *;
    *     groups. Highlight the step and run the selected     *;
    *     code.                                               *;
    *  2) Notice the DATA step includes IF-THEN/ELSE          *;
    *     statements to create a new column named BasinGroup. *;
    *  3) Delete the IF-THEN/ELSE statements and replace it   *;
    *     with an assignment statement to create the          *;
    *     BasinGroup column. Use the PUT function with Basin  *;
    *     as the first argument and $REGION. as the second    *;
    *     argument.                                           *;
    *  4) Highlight the DATA and PROC MEANS steps and run the *;
    *     selected code. How many BasinGroup values are in    *;
    *     the summary report?                                 *;
    ***********************************************************;
     
    proc format;
        value $region 'NA'='Atlantic'
                      'WP','EP','SP'='Pacific'
                      'NI','SI'='Indian'
                      ' '='Missing'
                      other='Unknown';
    run;
     
    data storm_summary;
        set pg2.storm_summary;
        Basin=upcase(Basin);
        BasinGroup=Basin; 
        format BasinGroup $region.;
        run;
      
    /*   *or */
      
      
    data storm_summary;
        set pg2.storm_summary;
        Basin=upcase(Basin);
        BasinGroup=put(Basin, $region.); 
        
        run;
          
        
    /*     *Delete the IF-THEN/ELSE statements and replace them with an assignment statement; */
    /*     if Basin='NA' then BasinGroup='Atlantic'; */
    /*     else if Basin in ('WP','EP','SP') then BasinGroup='Pacific'; */
    /*     else if Basin in ('NI','SI') then BasinGroup='Indian'; */
    /*     else if Basin=' ' then BasinGroup='Missing'; */
    /*     else BasinGroup='Unknown'; */
     
     
    proc means data=storm_summary maxdec=1;
    class BasinGroup;
    var MaxWindMPH MinPressure;
    run;

     

3 REPLIES 3
PaigeMiller
Diamond | Level 26
BasinGroup=put(Basin, $region.); 

 

PUT writes the value of BASIN using the $REGION format and assigns this value to variable BASINGROUP. 

 

So if BASIN contains value 'NA' then BasinGroup contains the value 'Atlantic'. I don't know what "no conversion" in your title means; here 'NA' is, in a manner of speaking, converted to 'Atlantic'.

 

In the other code, where there is no PUT statement, the format $REGION is applied to variable BASINGROUP, and so the value 'NA' remains the value, but you see 'Atlantic'. (Please note that in the other code with the PUT statement, BASINGROUP has value 'Atlantic') That's what formats do, they change the appearance, but do not change the underlying value.

 

So, why use PUT? To change the actual value; using a format does not change the actual value, formats change the appearance of the value. This can make a difference in sorting, if you want a specific sort order, the two possibilities could sort differently. There are probably other reasons as well, but without knowing the real use of this data (because this is a contrived example), I can't be more specific.

 

Please post code inside a code box as I did above; please click on the "little running man" icon to open a code box.

--
Paige Miller
hmlong25
Obsidian | Level 7
Thank you for your answer.
I had previously used Put to convert numeric to character values. Input for character to numeric. When I came across this use of Put I see this is a different use of it....since this is for all character values. That was my question.
Tom
Super User Tom
Super User

Formats convert values into text.  Numeric formats convert numbers into character string.  Character formats convert character strings into (possibly different) character strings.

 

Informats convert text into values.  Numeric informats convert character strings into numbers.  Character informats convert character strings into (possibly different) character strings.

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
  • 3 replies
  • 1161 views
  • 4 likes
  • 3 in conversation