Skip to content

clindocu/hello-world

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

hello-world

SAS, R, Python

SAS

/********** Create Database db1 with String 'Hello World' */
data db1;
    ********** Format Variable String as character with lenght 11;
    format STRING $11.;
    STRING="Hello World";
    ********** Output in SAS log;
    put STRING;
    ********** Output in db1;
    output;
run;

title1 "Hello World";
proc print data=db1 noobs;
run;

SAS Macro

%macro Hello(MYWORD);
;/********** Create Database db1 with String 'Hello World' */ 
data db1;
    %********** Format Variable String as character with length 40;
    format STRING $40.;
    STRING=&MYWORD;
    %********** Output in SAS log with %put;
    %put &MYWORD;
    %********** Output in db1;
    output;
run;

title1 "Hello World";
proc print data=db1 noobs;
run;
%mend Hello;

%Hello("Hello World (generated with SAS Macro)");

SAS Macro with Loop

* Macro for printing Hello World n-times (and Output in SAS log);
%macro MyLoop(NumLoop=, Output=Y);
    %********** Declare iterator as local;
    %local I;

    %********** Upcase Output;
    %let Output=%upcase(&Output);

    %********** If Parameter Output is Y = Yes:;
    %if &Output=Y %then %do;
        %********** Loop;
        %do I=1 %to &NumLoop;
            %put "Hello World &I"; %* Output in SAS Log;
        %end;
        %end;

    %********** If Parameter Output is not Y;
    %else %do;
        %put "Output is not Y"; %* Output in SAS Log;
    %end;
%mend MyLoop;
 
%MyLoop(NumLoop=8, Output=y);

* NOTE: Declare all variables within macro as %local, avoid using %global variables;
* NOTE: Macro parameters are always local to the macro that defines them;

R

# Define String
string <- "Hello World"
# print
print(string)
# or
cat(string, "\n")

R Function

hello <- function(string) {
  cat(string, "\n")  
}

hello("Hello World (generated with R Function)")

R Function with Loop

# Function for printing Hello World n-times (if output = TRUE)
hello <- function(numloop, output = TRUE) {
  # Output if variable is TRUE
  if (output == TRUE) {
    for (i in 1:numloop) {
      # Print Hello World
      cat("Hello World", i, "\n")  
    }
  } else {
    cat("output is not TRUE", "\n")  
  }
}

hello(8, output = TRUE)

Python

string = "Hello World"
print(string)

Python Function

# Define function hello with parameter string
def hello(string):                       # Function header
    """Prints a string"""                # Description
    
    # Print string
    print(string)                        # Body

# Call hello
hello("Hello World (generated with Python Function)")

Python Function with Loop

# Define function hello for printing Hello World n-times (if output = TRUE)
def hello(numloop, output = True):
    """Prints Hello World n-times"""

    # If Output is true
    if output is True:
       # Loop
       for i in range(numloop):
           # Print string
           print("Hello World", i+1)
    else:
       print("Output is not TRUE")

# Call hello
hello(8, output = True)

About

Hello World. SAS, R, Python

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published